home *** CD-ROM | disk | FTP | other *** search
/ Acorn RISC PD-CD 1 / Acorn RISC PD-CD 1.iso / games / _gnuchess / src / c / gnuchess next >
Encoding:
Text File  |  1994-02-25  |  76.5 KB  |  3,398 lines

  1. /*
  2.   gnuchess.c - C source for GNU CHESS
  3.  
  4.   Revision: 1990-04-18
  5.  
  6.   Copyright (C) 1986, 1987, 1988, 1989, 1990 Free Software Foundation, Inc.
  7.   Copyright (c) 1988, 1989, 1990  John Stanback
  8.  
  9.   This file is part of CHESS.
  10.  
  11.   CHESS is distributed in the hope that it will be useful, but WITHOUT ANY
  12.   WARRANTY.  No author or distributor accepts responsibility to anyone for
  13.   the consequences of using it or for whether it serves any particular
  14.   purpose or works at all, unless he says so in writing.  Refer to the CHESS
  15.   General Public License for full details.
  16.  
  17.   Everyone is granted permission to copy, modify and redistribute CHESS, but
  18.   only under the conditions described in the CHESS General Public License.
  19.   A copy of this license is supposed to have been given to you along with
  20.   CHESS so you can know your rights and responsibilities.  It should be in a
  21.   file named COPYING.  Among other things, the copyright notice and this
  22.   notice must be preserved on all copies.
  23. */
  24.  
  25.  
  26. #include "gnuchess.h"
  27.  
  28. #include <ctype.h>
  29.  
  30. #ifdef MSDOS
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <time.h>
  34. #define RWA_ACC "r+b"
  35. #define WA_ACC "w+b"
  36. #else
  37. #define RWA_ACC "r+"
  38. #define WA_ACC "w+"
  39. #include <sys/param.h>
  40. #include <sys/types.h>
  41. #ifndef __arm__
  42. #include <sys/times.h>
  43. #else
  44. #include <errno.h>
  45. #endif /*__arm__*/
  46. #endif /* MSDOS */
  47.  
  48. /* <stdlib.h> */
  49. extern int abs (int);
  50. extern int atoi (const char *);
  51. /* <time.h> */
  52. extern long int time (long int *);
  53. /* <string.h> */
  54. extern void *memset (void *, int, size_t);
  55.  
  56. #define valueP 100
  57. #define valueN 350
  58. #define valueB 355
  59. #define valueR 550
  60. #define valueQ 1100
  61. #define valueK 1200
  62. #define ctlP 0x4000
  63. #define ctlN 0x2800
  64. #define ctlB 0x1800
  65. #define ctlR 0x0400
  66. #define ctlQ 0x0200
  67. #define ctlK 0x0100
  68. #define ctlBQ 0x1200
  69. #define ctlBN 0x0800
  70. #define ctlRQ 0x0600
  71. #define ctlNN 0x2000
  72. #define Patak(c, u) (atak[c][u] > ctlP)
  73. #define Anyatak(c, u) (atak[c][u] > 0)
  74.  
  75. #if ttblsz
  76. #define truescore 0x0001
  77. #define lowerbound 0x0002
  78. #define upperbound 0x0004
  79. #define kingcastle 0x0008
  80. #define queencastle 0x0010
  81.  
  82. struct hashval
  83. {
  84.   unsigned long key,bd;
  85. };
  86. struct hashentry
  87. {
  88.   unsigned long hashbd;
  89.   unsigned short mv;
  90.   unsigned char flags, depth;    /* char saves some space */
  91.   short score;
  92. #ifdef HASHTEST
  93.   unsigned char bd[32];
  94. #endif /* HASHTEST */
  95.  
  96. };
  97.  
  98. #ifdef HASHFILE
  99. /*
  100.   persistent transposition table.
  101.   The size must be a power of 2. If you change the size,
  102.   be sure to run gnuchess -t before anything else.
  103. */
  104. #define frehash 6
  105. #ifdef MSDOS
  106. #define filesz (1 << 11)
  107. #else
  108. #define filesz (1 << 17)
  109. #endif /* MSDOS */
  110. struct fileentry
  111. {
  112.   unsigned char bd[32];
  113.   unsigned char f, t, flags, depth, sh, sl;
  114. };
  115. /*
  116.   In a networked enviroment gnuchess might be compiled on different
  117.   hosts with different random number generators, that is not acceptable
  118.   if they are going to share the same transposition table.
  119. */
  120. unsigned long int next = 1;
  121.  
  122. unsigned int urand (void)
  123. {
  124.   next *= 1103515245;
  125.   next += 12345;
  126.   return ((unsigned int) (next >> 16) & 0xFFFF);
  127. }
  128.  
  129. void srand (unsigned int seed)
  130. {
  131.   next = seed;
  132. }
  133. #else
  134. #define urand rand
  135. #endif /* HASHFILE */
  136.  
  137. static unsigned long hashkey, hashbd;
  138. static struct hashval hashcode[2][7][64];
  139. static struct hashentry huge ttable[2][ttblsz];
  140. #endif /* ttblsz */
  141. static short rpthash[2][256];
  142.  
  143. FILE *hashfile;
  144. struct leaf Tree[2000], *root;
  145. short TrPnt[maxdepth];
  146. short PieceList[2][16], PawnCnt[2][8];
  147. #define wking PieceList[white][0]
  148. #define bking PieceList[black][0]
  149. #define EnemyKing PieceList[c2][0]
  150. short castld[2], Mvboard[64];
  151. short svalue[64];
  152. struct flags flag;
  153. short opponent, computer, Awindow, Bwindow, dither, INCscore;
  154. long ResponseTime, ExtraTime, Level, et, et0, time0, ft;
  155. long NodeCnt, ETnodes, EvalNodes, HashCnt, FHashCnt, HashCol;
  156. short player, xwndw, rehash;
  157. struct GameRec GameList[512];
  158. short Sdepth, GameCnt, Game50, MaxSearchDepth;
  159. short epsquare, contempt;
  160. struct BookEntry *Book;
  161. struct TimeControlRec TimeControl;
  162. short TCflag, TCmoves, TCminutes, OperatorTime;
  163. const short otherside[3] =
  164. {1, 0, 2};
  165. unsigned short hint, PrVar[maxdepth];
  166.  
  167. static short Pindex[64];
  168. static short PieceCnt[2];
  169. static short c1, c2, *atk1, *atk2, *PC1, *PC2, atak[2][64];
  170. static short mtl[2], pmtl[2], emtl[2], hung[2];
  171. static short FROMsquare, TOsquare, Zscore, zwndw;
  172. static short HasKnight[2], HasBishop[2], HasRook[2], HasQueen[2];
  173. static short ChkFlag[maxdepth], CptrFlag[maxdepth], PawnThreat[maxdepth];
  174. static short Pscore[maxdepth], Tscore[maxdepth];
  175. static const short qrook[3] =
  176. {0, 56, 0};
  177. static const short krook[3] =
  178. {7, 63, 0};
  179. static const short kingP[3] =
  180. {4, 60, 0};
  181. static const short rank7[3] =
  182. {6, 1, 0};
  183. static const short sweep[8] =
  184. {false, false, false, true, true, true, false, false};
  185. static unsigned short killr0[maxdepth], killr1[maxdepth];
  186. static unsigned short killr2[maxdepth], killr3[maxdepth];
  187. static unsigned short PV, Swag0, Swag1, Swag2, Swag3, Swag4;
  188. static unsigned char history[8192];
  189.  
  190. static short Mwpawn[64], Mbpawn[64], Mknight[2][64], Mbishop[2][64];
  191. static short Mking[2][64], Kfield[2][64];
  192. static const short value[7] =
  193. {0, valueP, valueN, valueB, valueR, valueQ, valueK};
  194. static const short control[7] =
  195. {0, ctlP, ctlN, ctlB, ctlR, ctlQ, ctlK};
  196. static const short PassedPawn0[8] =
  197. {0, 60, 80, 120, 200, 360, 600, 800};
  198. static const short PassedPawn1[8] =
  199. {0, 30, 40, 60, 100, 180, 300, 800};
  200. static const short PassedPawn2[8] =
  201. {0, 15, 25, 35, 50, 90, 140, 800};
  202. static const short PassedPawn3[8] =
  203. {0, 5, 10, 15, 20, 30, 140, 800};
  204. static const short ISOLANI[8] =
  205. {-12, -16, -20, -24, -24, -20, -16, -12};
  206. static const short BACKWARD[16] =
  207. {-6, -10, -15, -21, -28, -28, -28, -28,
  208.  -28, -28, -28, -28, -28, -28, -28, -28};
  209. static const short BMBLTY[14] =
  210. {-2, 0, 2, 4, 6, 8, 10, 12, 13, 14, 15, 16, 16, 16};
  211. static const short RMBLTY[15] =
  212. {0, 2, 4, 6, 8, 10, 11, 12, 13, 14, 14, 14, 14, 14, 14};
  213. static const short KTHRT[36] =
  214. {0, -8, -20, -36, -52, -68, -80, -80, -80, -80, -80, -80,
  215.  -80, -80, -80, -80, -80, -80, -80, -80, -80, -80, -80, -80,
  216.  -80, -80, -80, -80, -80, -80, -80, -80, -80, -80, -80, -80};
  217. static short KNIGHTPOST, KNIGHTSTRONG, BISHOPSTRONG, KATAK;
  218. static short PEDRNK2B, PWEAKH, PADVNCM, PADVNCI, PAWNSHIELD, PDOUBLED, PBLOK;
  219. static short RHOPN, RHOPNX, KHOPN, KHOPNX, KSFTY;
  220. static short ATAKD, HUNGP, HUNGX, KCASTLD, KMOVD, XRAY, PINVAL;
  221. static short stage, stage2, Developed[2];
  222. static short PawnBonus, BishopBonus, RookBonus;
  223. static const short KingOpening[64] =
  224. {0, 0, -4, -10, -10, -4, 0, 0,
  225.  -4, -4, -8, -12, -12, -8, -4, -4,
  226.  -12, -16, -20, -20, -20, -20, -16, -12,
  227.  -16, -20, -24, -24, -24, -24, -20, -16,
  228.  -16, -20, -24, -24, -24, -24, -20, -16,
  229.  -12, -16, -20, -20, -20, -20, -16, -12,
  230.  -4, -4, -8, -12, -12, -8, -4, -4,
  231.  0, 0, -4, -10, -10, -4, 0, 0};
  232. static const short KingEnding[64] =
  233. {0, 6, 12, 18, 18, 12, 6, 0,
  234.  6, 12, 18, 24, 24, 18, 12, 6,
  235.  12, 18, 24, 30, 30, 24, 18, 12,
  236.  18, 24, 30, 36, 36, 30, 24, 18,
  237.  18, 24, 30, 36, 36, 30, 24, 18,
  238.  12, 18, 24, 30, 30, 24, 18, 12,
  239.  6, 12, 18, 24, 24, 18, 12, 6,
  240.  0, 6, 12, 18, 18, 12, 6, 0};
  241. static const short DyingKing[64] =
  242. {0, 8, 16, 24, 24, 16, 8, 0,
  243.  8, 32, 40, 48, 48, 40, 32, 8,
  244.  16, 40, 56, 64, 64, 56, 40, 16,
  245.  24, 48, 64, 72, 72, 64, 48, 24,
  246.  24, 48, 64, 72, 72, 64, 48, 24,
  247.  16, 40, 56, 64, 64, 56, 40, 16,
  248.  8, 32, 40, 48, 48, 40, 32, 8,
  249.  0, 8, 16, 24, 24, 16, 8, 0};
  250. static const short KBNK[64] =
  251. {99, 90, 80, 70, 60, 50, 40, 40,
  252.  90, 80, 60, 50, 40, 30, 20, 40,
  253.  80, 60, 40, 30, 20, 10, 30, 50,
  254.  70, 50, 30, 10, 0, 20, 40, 60,
  255.  60, 40, 20, 0, 10, 30, 50, 70,
  256.  50, 30, 10, 20, 30, 40, 60, 80,
  257.  40, 20, 30, 40, 50, 60, 80, 90,
  258.  40, 40, 50, 60, 70, 80, 90, 99};
  259. static const short pknight[64] =
  260. {0, 4, 8, 10, 10, 8, 4, 0,
  261.  4, 8, 16, 20, 20, 16, 8, 4,
  262.  8, 16, 24, 28, 28, 24, 16, 8,
  263.  10, 20, 28, 32, 32, 28, 20, 10,
  264.  10, 20, 28, 32, 32, 28, 20, 10,
  265.  8, 16, 24, 28, 28, 24, 16, 8,
  266.  4, 8, 16, 20, 20, 16, 8, 4,
  267.  0, 4, 8, 10, 10, 8, 4, 0};
  268. static const short pbishop[64] =
  269. {14, 14, 14, 14, 14, 14, 14, 14,
  270.  14, 22, 18, 18, 18, 18, 22, 14,
  271.  14, 18, 22, 22, 22, 22, 18, 14,
  272.  14, 18, 22, 22, 22, 22, 18, 14,
  273.  14, 18, 22, 22, 22, 22, 18, 14,
  274.  14, 18, 22, 22, 22, 22, 18, 14,
  275.  14, 22, 18, 18, 18, 18, 22, 14,
  276.  14, 14, 14, 14, 14, 14, 14, 14};
  277. static const short PawnAdvance[64] =
  278. {0, 0, 0, 0, 0, 0, 0, 0,
  279.  4, 4, 4, 0, 0, 4, 4, 4,
  280.  6, 8, 2, 10, 10, 2, 8, 6,
  281.  6, 8, 12, 16, 16, 12, 8, 6,
  282.  8, 12, 16, 24, 24, 16, 12, 8,
  283.  12, 16, 24, 32, 32, 24, 16, 12,
  284.  12, 16, 24, 32, 32, 24, 16, 12,
  285.  0, 0, 0, 0, 0, 0, 0, 0};
  286.  
  287.  
  288. /* .... MOVE GENERATION VARIABLES AND INITIALIZATIONS .... */
  289.  
  290.  
  291. #define taxicab(a,b) taxidata[a][b]
  292. short distdata[64][64], taxidata[64][64];
  293.  
  294. static inline void
  295. Initialize_dist (void)
  296. {
  297.   register short a, b, d, di;
  298.  
  299.   for (a = 0; a < 64; a++)
  300.     for (b = 0; b < 64; b++)
  301.       {
  302.     d = abs (column (a) - column (b));
  303.     di = abs (row (a) - row (b));
  304.     taxidata[a][b] = d + di;
  305.     distdata[a][b] = (d > di ? d : di);
  306.       }
  307. }
  308.  
  309. const short Stboard[64] =
  310. {rook, knight, bishop, queen, king, bishop, knight, rook,
  311.  pawn, pawn, pawn, pawn, pawn, pawn, pawn, pawn,
  312.  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  313.  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  314.  pawn, pawn, pawn, pawn, pawn, pawn, pawn, pawn,
  315.  rook, knight, bishop, queen, king, bishop, knight, rook};
  316. const short Stcolor[64] =
  317. {white, white, white, white, white, white, white, white,
  318.  white, white, white, white, white, white, white, white,
  319.  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  320.  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  321.  black, black, black, black, black, black, black, black,
  322.  black, black, black, black, black, black, black, black};
  323. short board[64], color[64];
  324. static unsigned char nextpos[8][64][64];
  325. static unsigned char nextdir[8][64][64];
  326. /*
  327.   ptype is used to separate white and black pawns, like this;
  328.   ptyp = ptype[side][piece]
  329.   piece can be used directly in nextpos/nextdir when generating moves
  330.   for pieces that are not black pawns.
  331. */
  332. static const short ptype[2][8] =
  333. {
  334.   no_piece, pawn, knight, bishop, rook, queen, king, no_piece,
  335.   no_piece, bpawn, knight, bishop, rook, queen, king, no_piece};
  336. static const short direc[8][8] =
  337. {
  338.   0, 0, 0, 0, 0, 0, 0, 0,
  339.   10, 9, 11, 0, 0, 0, 0, 0,
  340.   8, -8, 12, -12, 19, -19, 21, -21,
  341.   9, 11, -9, -11, 0, 0, 0, 0,
  342.   1, 10, -1, -10, 0, 0, 0, 0,
  343.   1, 10, -1, -10, 9, 11, -9, -11,
  344.   1, 10, -1, -10, 9, 11, -9, -11,
  345.   -10, -9, -11, 0, 0, 0, 0, 0};
  346. static const short max_steps[8] =
  347. {0, 2, 1, 7, 7, 7, 1, 2};
  348. static const short nunmap[120] =
  349. {
  350.   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  351.   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  352.   -1, 0, 1, 2, 3, 4, 5, 6, 7, -1,
  353.   -1, 8, 9, 10, 11, 12, 13, 14, 15, -1,
  354.   -1, 16, 17, 18, 19, 20, 21, 22, 23, -1,
  355.   -1, 24, 25, 26, 27, 28, 29, 30, 31, -1,
  356.   -1, 32, 33, 34, 35, 36, 37, 38, 39, -1,
  357.   -1, 40, 41, 42, 43, 44, 45, 46, 47, -1,
  358.   -1, 48, 49, 50, 51, 52, 53, 54, 55, -1,
  359.   -1, 56, 57, 58, 59, 60, 61, 62, 63, -1,
  360.   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  361.   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
  362.  
  363.  
  364. void
  365. Initialize_moves (void)
  366.  
  367. /*
  368.   This procedure pre-calculates all moves for every piece from every square.
  369.   This data is stored in nextpos/nextdir and used later in the move generation
  370.   routines.
  371. */
  372.  
  373. {
  374.   short ptyp, po, p0, d, di, s, delta;
  375.   unsigned char *ppos, *pdir;
  376.   short dest[8][8];
  377.   short steps[8];
  378.   short sorted[8];
  379.  
  380.   for (ptyp = 0; ptyp < 8; ptyp++)
  381.     for (po = 0; po < 64; po++)
  382.       for (p0 = 0; p0 < 64; p0++)
  383.     {
  384.       nextpos[ptyp][po][p0] = (unsigned char) po;
  385.       nextdir[ptyp][po][p0] = (unsigned char) po;
  386.     }
  387.   for (ptyp = 1; ptyp < 8; ptyp++)
  388.     for (po = 21; po < 99; po++)
  389.       if (nunmap[po] >= 0)
  390.     {
  391.       ppos = nextpos[ptyp][nunmap[po]];
  392.       pdir = nextdir[ptyp][nunmap[po]];
  393.       /* dest is a function of direction and steps */
  394.       for (d = 0; d < 8; d++)
  395.         {
  396.           dest[d][0] = nunmap[po];
  397.           delta = direc[ptyp][d];
  398.           if (delta != 0)
  399.         {
  400.           p0 = po;
  401.           for (s = 0; s < max_steps[ptyp]; s++)
  402.             {
  403.               p0 = p0 + delta;
  404.               /*
  405.             break if (off board) or
  406.             (pawns only move two steps from home square)
  407.               */
  408.               if (nunmap[p0] < 0 || (ptyp == pawn || ptyp == bpawn)
  409.               && s > 0 && (d > 0 || Stboard[nunmap[po]] != pawn))
  410.             break;
  411.               else
  412.             dest[d][s] = nunmap[p0];
  413.             }
  414.         }
  415.           else
  416.         s = 0;
  417.  
  418.           /*
  419.             sort dest in number of steps order
  420.             currently no sort is done due to compability with
  421.             the move generation order in old gnu chess
  422.           */
  423.           steps[d] = s;
  424.           for (di = d; s > 0 && di > 0; di--)
  425.         if (steps[sorted[di - 1]] == 0)    /* should be: < s */
  426.           sorted[di] = sorted[di - 1];
  427.         else
  428.           break;
  429.           sorted[di] = d;
  430.         }
  431.  
  432.       /*
  433.         update nextpos/nextdir,
  434.         pawns have two threads (capture and no capture)
  435.       */
  436.       p0 = nunmap[po];
  437.       if (ptyp == pawn || ptyp == bpawn)
  438.         {
  439.           for (s = 0; s < steps[0]; s++)
  440.         {
  441.           ppos[p0] = (unsigned char) dest[0][s];
  442.           p0 = dest[0][s];
  443.         }
  444.           p0 = nunmap[po];
  445.           for (d = 1; d < 3; d++)
  446.         {
  447.           pdir[p0] = (unsigned char) dest[d][0];
  448.           p0 = dest[d][0];
  449.         }
  450.         }
  451.       else
  452.         {
  453.           pdir[p0] = (unsigned char) dest[sorted[0]][0];
  454.           for (d = 0; d < 8; d++)
  455.         for (s = 0; s < steps[sorted[d]]; s++)
  456.           {
  457.             ppos[p0] = (unsigned char) dest[sorted[d]][s];
  458.             p0 = dest[sorted[d]][s];
  459.             if (d < 7)
  460.               pdir[p0] = (unsigned char) dest[sorted[d + 1]][0];
  461.             /* else is already initialized */
  462.           }
  463.         }
  464.     }
  465. }
  466.  
  467. /* hmm.... shouldn`t main be moved to the interface routines */
  468. int
  469. main (int argc, char **argv)
  470. {
  471.   short int ahead = true, hash = true;
  472.   char *xwin = 0;
  473.  
  474.   while (argc > 1 && ((argv[1][0] == '-') || (argv[1][0] == '+')))
  475.     {
  476.       switch (argv[1][1])
  477.     {
  478.     case 'a':
  479.       ahead = (argv[1][0] == '-') ? false : true;
  480.       break;
  481.     case 'h':
  482.       hash = (argv[1][0] == '-') ? false : true;
  483.       break;
  484. #if ttblsz
  485. #ifdef HASHFILE
  486.     case 't': /* create or test persistent transposition table */ 
  487.       {
  488.         if ((hashfile = fopen (HASHFILE, RWA_ACC)) == NULL)
  489.           hashfile = fopen (HASHFILE , WA_ACC);
  490.         if (hashfile != NULL)
  491.           {
  492.         long i, j;
  493.         int nr[maxdepth];
  494.         struct fileentry n;
  495.         
  496.         printf("Counting transposition file entries, wait!\n");
  497.         for(i = 0; i < maxdepth; i++) nr[i] = 0;
  498.         fseek(hashfile, 0L, SEEK_END);
  499.         i = ftell(hashfile) / sizeof(struct fileentry);
  500.         fseek(hashfile, 0L, SEEK_SET);
  501.         for (j = 0; j < i; j++)
  502.           {
  503.             fread(&n, sizeof(struct fileentry), 1, hashfile);
  504.             if (n.depth)
  505.               {
  506.             nr[n.depth]++;
  507.             nr[0]++;
  508.               }
  509.           }
  510.         printf("The file contains %d entries out of max %d\n",
  511.                nr[0], i);
  512.         for(j = 1; j < maxdepth; j++) printf("%d ",nr[j]);
  513.         printf("\n");
  514.         if (i < filesz)
  515.           {
  516.             printf("Filling transposition file, wait!\n");
  517.             for(j = 0; j < 32; j++)
  518.               n.bd[j] = 0;
  519.             n.f = n.t = 0;
  520.             n.flags = 0;
  521.             n.depth = 0;
  522.             n.sh = n.sl = 0;
  523.             for (j = i; j < filesz; j++)
  524.               fwrite (&n, sizeof(struct fileentry), 1, hashfile);
  525.           }
  526.         fclose(hashfile);
  527.           }
  528.         return(0);
  529.         break;
  530.       }
  531. #endif /* HASHFILE */
  532. #endif /* ttblsz */
  533.     case 'x':
  534.       xwin = &argv[1][2];
  535.       break;
  536.     default:
  537.       fprintf (stderr, "Usage: gnuchess [-a] [-t] [-x xwndw]\n");
  538.     }
  539.       argv++;
  540.       argc--;
  541.     }
  542.   Level = 0;
  543.   TCflag = false;
  544.   OperatorTime = 0;
  545.   if (argc == 2)
  546.     Level = atoi (argv[1]);
  547.   if (argc == 3)
  548.     {
  549.       TCmoves = atoi (argv[1]);
  550.       TCminutes = atoi (argv[2]);
  551.       TCflag = true;
  552.     }
  553.   Initialize ();
  554.   Initialize_dist ();
  555.   Initialize_moves ();
  556.   NewGame ();
  557.   GetOpenings ();
  558.  
  559.   flag.easy = ahead;
  560.   flag.hash = hash;
  561.   if (xwin)
  562.     xwndw = atoi (xwin);
  563.  
  564.   hashfile = NULL;
  565. #if ttblsz
  566. #ifdef HASHFILE
  567.   hashfile = fopen (HASHFILE, RWA_ACC);
  568. #endif /* HASHFILE */
  569. #endif  /* ttblsz */
  570.   while (!(flag.quit))
  571.     {
  572.       if (flag.bothsides && !flag.mate)
  573.     SelectMove (opponent, 1);
  574.       else
  575.     InputCommand ();
  576.       if (!(flag.quit || flag.mate || flag.force))
  577.     SelectMove (computer, 1);
  578.     }
  579. #if ttblsz
  580. #ifdef HASHFILE
  581.   if (hashfile) fclose(hashfile);
  582. #endif /* HASHFILE */
  583. #endif /* ttblsz */
  584.  
  585.   ExitChess ();
  586.   return (0);
  587. }
  588.  
  589. void
  590. NewGame (void)
  591.  
  592. /*
  593.   Reset the board and other variables to start a new game.
  594. */
  595.  
  596. {
  597.   short l, c, p;
  598.  
  599.   stage = stage2 = -1;        /* the game is not yet started */
  600.   flag.mate = flag.post = flag.quit = flag.reverse = flag.bothsides = false;
  601.   flag.force = false;
  602.   flag.hash = flag.easy = flag.beep = flag.rcptr = true;
  603.   NodeCnt = et0 = epsquare = 0;
  604.   dither = 0;
  605.   Awindow = 90;
  606.   Bwindow = 90;
  607.   xwndw = 90;
  608.   MaxSearchDepth = 29;
  609.   contempt = 0;
  610.   GameCnt = 0;
  611.   Game50 = 1;
  612.   hint = 0x0C14;
  613.   ZeroRPT ();
  614.   Developed[white] = Developed[black] = false;
  615.   castld[white] = castld[black] = false;
  616.   PawnThreat[0] = CptrFlag[0] = false;
  617.   Pscore[0] = 12000;
  618.   Tscore[0] = 12000;
  619.   opponent = white;
  620.   computer = black;
  621.   for (l = 0; l < 2000; l++)
  622.     Tree[l].f = Tree[l].t = 0;
  623. #if ttblsz
  624.   rehash = 6;
  625.   ZeroTTable ();
  626.   srand ((unsigned int) 1);
  627.   for (c = white; c <= black; c++)
  628.     for (p = pawn; p <= king; p++)
  629.       for (l = 0; l < 64; l++)
  630.     {
  631.       hashcode[c][p][l].key = (((unsigned long) urand ()));
  632.       hashcode[c][p][l].key += (((unsigned long) urand ()) << 16);
  633.       hashcode[c][p][l].bd = (((unsigned long) urand ()));
  634.       hashcode[c][p][l].bd += (((unsigned long) urand ()) << 16);
  635.       if (sizeof(long) > 4)
  636.         {
  637.           hashcode[c][p][l].key += (((unsigned long) urand ()) << 32);
  638.           hashcode[c][p][l].key += (((unsigned long) urand ()) << 48);
  639.           hashcode[c][p][l].bd += (((unsigned long) urand ()) << 32);
  640.           hashcode[c][p][l].bd += (((unsigned long) urand ()) << 48);
  641.         }
  642.     }
  643. #endif /* ttblsz */
  644.   for (l = 0; l < 64; l++)
  645.     {
  646.       board[l] = Stboard[l];
  647.       color[l] = Stcolor[l];
  648.       Mvboard[l] = 0;
  649.     }
  650.   ClrScreen ();
  651.   if (TCflag)
  652.     SetTimeControl ();
  653.   else if (Level == 0)
  654.     SelectLevel ();
  655.   InitializeStats ();
  656.   time0 = time ((long *) 0);
  657.   ElapsedTime (1);
  658.   UpdateDisplay (0, 0, 1, 0);
  659. }
  660.  
  661.  
  662. /* ............    MOVE GENERATION & SEARCH ROUTINES    .............. */
  663.  
  664. static inline void
  665. pick (short int p1, short int p2)
  666.  
  667. /*
  668.   Find the best move in the tree between indexes p1 and p2. Swap the best
  669.   move into the p1 element.
  670. */
  671.  
  672. {
  673.   register short p, s;
  674.   short p0, s0;
  675.   struct leaf temp;
  676.  
  677.   s0 = Tree[p1].score;
  678.   p0 = p1;
  679.   for (p = p1 + 1; p <= p2; p++)
  680.     if ((s = Tree[p].score) > s0)
  681.       {
  682.     s0 = s;
  683.     p0 = p;
  684.       }
  685.   if (p0 != p1)
  686.     {
  687.       temp = Tree[p1];
  688.       Tree[p1] = Tree[p0];
  689.       Tree[p0] = temp;
  690.     }
  691. }
  692.  
  693. void
  694. SelectMove (short int side, short int iop)
  695.  
  696.  
  697. /*
  698.   Select a move by calling function search() at progressively deeper ply
  699.   until time is up or a mate or draw is reached. An alpha-beta window of -90
  700.   to +90 points is set around the score returned from the previous
  701.   iteration. If Sdepth != 0 then the program has correctly predicted the
  702.   opponents move and the search will start at a depth of Sdepth+1 rather
  703.   than a depth of 1.
  704. */
  705.  
  706. {
  707.   static short i, tempb, tempc, tempsf, tempst, xside, rpt;
  708.   static short alpha, beta, score;
  709.  
  710.   flag.timeout = false;
  711.   xside = otherside[side];
  712.   if (iop != 2)
  713.     player = side;
  714.   if (TCflag)
  715.     {
  716.       if ((TimeControl.moves[side] + 3) != 0)
  717.     ResponseTime = (TimeControl.clock[side]) /
  718.       (TimeControl.moves[side] + 3) -
  719.       OperatorTime;
  720.       else
  721.     ResponseTime = 0;
  722.       ResponseTime += (ResponseTime * TimeControl.moves[side]) / (2 * TCmoves + 1);
  723.     }
  724.   else
  725.     ResponseTime = Level;
  726.   if (iop == 2)
  727.     ResponseTime = 99999;
  728.   if (Sdepth > 0 && root->score > Zscore - zwndw)
  729.     ResponseTime -= ft;
  730.   else if (ResponseTime < 1)
  731.     ResponseTime = 1;
  732.   ExtraTime = 0;
  733.   ExaminePosition ();
  734.   ScorePosition (side, &score);
  735.   /* Pscore[0] = -score; */
  736.   ShowSidetomove ();
  737.  
  738.   if (Sdepth == 0)
  739.     {
  740. #if ttblsz
  741.       /* ZeroTTable (); */
  742. #endif /* ttblsz */
  743.       SearchStartStuff (side);
  744. #ifdef NOMEMSET
  745.       for (i = 0; i < 8192; i++)
  746.     history[i] = 0;
  747. #else
  748.       memset ((char *) history, 0, sizeof (history));
  749. #endif /* NOMEMSET */
  750.       FROMsquare = TOsquare = -1;
  751.       PV = 0;
  752.       if (iop != 2)
  753.     hint = 0;
  754.       for (i = 0; i < maxdepth; i++)
  755.     PrVar[i] = killr0[i] = killr1[i] = killr2[i] = killr3[i] = 0;
  756.       alpha = score - 90;
  757.       beta = score + 90;
  758.       rpt = 0;
  759.       TrPnt[1] = 0;
  760.       root = &Tree[0];
  761.       MoveList (side, 1);
  762.       for (i = TrPnt[1]; i < TrPnt[2]; i++)
  763.     pick (i, TrPnt[2] - 1);
  764.       if (Book != NULL)
  765.     OpeningBook (&hint);
  766.       if (Book != NULL)
  767.     flag.timeout = true;
  768.       NodeCnt = ETnodes = EvalNodes = HashCnt = FHashCnt = HashCol = 0;
  769.       Zscore = 0;
  770.       zwndw = 20;
  771.     }
  772.   while (!flag.timeout && Sdepth < MaxSearchDepth)
  773.     {
  774.       Sdepth++;
  775.       ShowDepth (' ');
  776.       score = search (side, 1, Sdepth, alpha, beta, PrVar, &rpt);
  777.       for (i = 1; i <= Sdepth; i++)
  778.     killr0[i] = PrVar[i];
  779.       if (score < alpha)
  780.     {
  781.       ShowDepth ('-');
  782.       ExtraTime = 10 * ResponseTime;
  783.       /* ZeroTTable (); */
  784.       score = search (side, 1, Sdepth, -9000, score, PrVar, &rpt);
  785.     }
  786.       if (score > beta && !(root->flags & exact))
  787.     {
  788.       ShowDepth ('+');
  789.       ExtraTime = 0;
  790.       /* ZeroTTable (); */
  791.       score = search (side, 1, Sdepth, score, 9000, PrVar, &rpt);
  792.     }
  793.       score = root->score;
  794.       if (!flag.timeout)
  795.     for (i = TrPnt[1] + 1; i < TrPnt[2]; i++)
  796.       pick (i, TrPnt[2] - 1);
  797.       ShowResults (score, PrVar, '.');
  798.       for (i = 1; i <= Sdepth; i++)
  799.     killr0[i] = PrVar[i];
  800.       if (score > Zscore - zwndw && score > Tree[1].score + 250)
  801.     ExtraTime = 0;
  802.       else if (score > Zscore - 3 * zwndw)
  803.     ExtraTime = ResponseTime;
  804.       else
  805.     ExtraTime = 3 * ResponseTime;
  806.       if (root->flags & exact)
  807.     flag.timeout = true;
  808.       if (Tree[1].score < -9000)
  809.     flag.timeout = true;
  810.       if (4 * et > 2 * ResponseTime + ExtraTime)
  811.     flag.timeout = true;
  812.       if (!flag.timeout)
  813.     {
  814.       Tscore[0] = score;
  815.       if (Zscore == 0)
  816.         Zscore = score;
  817.       else
  818.         Zscore = (Zscore + score) / 2;
  819.     }
  820.       zwndw = 20 + abs (Zscore / 12);
  821.       beta = score + Bwindow;
  822.       if (Zscore < score)
  823.     alpha = Zscore - Awindow - zwndw;
  824.       else
  825.     alpha = score - Awindow - zwndw;
  826.     }
  827.  
  828.   score = root->score;
  829.   if (rpt >= 2 || score < -12000)
  830.     root->flags |= draw;
  831.   if (iop == 2)
  832.     return;
  833.   if (Book == NULL)
  834.     hint = PrVar[2];
  835.   ElapsedTime (1);
  836.  
  837.   if (score > -9999 && rpt <= 2)
  838.     {
  839.       MakeMove (side, root, &tempb, &tempc, &tempsf, &tempst, &INCscore);
  840.       algbr (root->f, root->t, (short) root->flags);
  841.     }
  842.   else
  843.     algbr (0, 0, 0);
  844.   OutputMove ();
  845.   if (score == -9999 || score == 9998)
  846.     flag.mate = true;
  847.   if (flag.mate)
  848.     hint = 0;
  849.   if ((board[root->t] == pawn)
  850.       || (root->flags & capture)
  851.       || (root->flags & cstlmask))
  852.     {
  853.       Game50 = GameCnt;
  854.       ZeroRPT ();
  855.     }
  856.   GameList[GameCnt].score = score;
  857.   GameList[GameCnt].nodes = NodeCnt;
  858.   GameList[GameCnt].time = (short) et;
  859.   GameList[GameCnt].depth = Sdepth;
  860.   if (TCflag)
  861.     {
  862.       TimeControl.clock[side] -= (et + OperatorTime);
  863.       if (--TimeControl.moves[side] == 0)
  864.     SetTimeControl ();
  865.     }
  866.   if ((root->flags & draw) && flag.bothsides)
  867.     flag.mate = true;
  868.   if (GameCnt > 470)
  869.     flag.mate = true; /* out of move store, you loose */
  870.   player = xside;
  871.   Sdepth = 0;
  872.   fflush (stdin);
  873. }
  874.  
  875. int
  876. parse (FILE *fd, short unsigned int *mv, short int side)
  877. {
  878.   int c, i, r1, r2, c1, c2;
  879.   char s[100];
  880.   while ((c = getc (fd)) == ' ') ;
  881.   i = 0;
  882.   s[0] = (char) c;
  883.   while (c != ' ' && c != '\n' && c != EOF)
  884.     s[++i] = (char) (c = getc (fd));
  885.   s[++i] = '\0';
  886.   if (c == EOF)
  887.     return (-1);
  888.   if (s[0] == '!' || s[0] == ';' || i < 3)
  889.     {
  890.       while (c != '\n' && c != EOF)
  891.     c = getc (fd);
  892.       return (0);
  893.     }
  894.   if (s[4] == 'o')
  895.     if (side == black)
  896.       *mv = 0x3C3A;
  897.     else
  898.       *mv = 0x0402;
  899.   else if (s[0] == 'o')
  900.     if (side == black)
  901.       *mv = 0x3C3E;
  902.     else
  903.       *mv = 0x0406;
  904.   else
  905.     {
  906.       c1 = s[0] - 'a';
  907.       r1 = s[1] - '1';
  908.       c2 = s[2] - 'a';
  909.       r2 = s[3] - '1';
  910.       *mv = (locn (r1, c1) << 8) | locn (r2, c2);
  911.     }
  912.   return (1);
  913. }
  914.  
  915. void
  916. GetOpenings (void)
  917.      
  918. /*
  919.    Read in the Opening Book file and parse the algebraic notation for a move
  920.    into an unsigned integer format indicating the from and to square. Create
  921.    a linked list of opening lines of play, with entry->next pointing to the
  922.    next line and entry->move pointing to a chunk of memory containing the
  923.    moves. More Opening lines of up to 256 half moves may be added to
  924.    gnuchess.book.
  925. */
  926. #ifndef BOOK
  927. #define BOOK "<gnuchess$dir>.gnuchess.book"
  928. #endif /* BOOK */     
  929. {
  930.   FILE *fd;
  931.   int c, i, j, side;
  932.   /* char buffr[2048]; */
  933.   struct BookEntry *entry;
  934.   unsigned short mv, *mp, tmp[100];
  935.  
  936.   if ((fd = fopen (BOOK, "r")) == NULL)
  937.     fd = fopen ("gnuchess.book", "r");
  938.   if (fd != NULL)
  939.     {
  940.       /* setvbuf(fd,buffr,_IOFBF,2048); */
  941.       Book = NULL;
  942.       i = 0;
  943.       side = white;
  944.       while ((c = parse (fd, &mv, side)) >= 0)
  945.     if (c == 1)
  946.       {
  947.         tmp[++i] = mv;
  948.         side = otherside[side];
  949.       }
  950.     else if (c == 0 && i > 0)
  951.       {
  952.         entry = (struct BookEntry *) malloc (sizeof (struct BookEntry));
  953.         mp = (unsigned short *) malloc ((i + 1) * sizeof (unsigned short));
  954.         if (!entry || !mp)
  955.           {
  956.         Book = NULL;
  957.         ShowMessage ("warning: can't load book, out of memory.");
  958.         return;
  959.           }
  960.         entry->mv = mp;
  961.         entry->next = Book;
  962.         Book = entry;
  963.         for (j = 1; j <= i; j++)
  964.           *(mp++) = tmp[j];
  965.         *mp = 0;
  966.         i = 0;
  967.         side = white;
  968.       }
  969.       fclose (fd);
  970.     }
  971.   else
  972.     ShowMessage ("warning: can't find book.");
  973. }
  974.  
  975.  
  976. void
  977. OpeningBook (unsigned short *hint)
  978.  
  979. /*
  980.   Go thru each of the opening lines of play and check for a match with the
  981.   current game listing. If a match occurs, generate a random number. If this
  982.   number is the largest generated so far then the next move in this line
  983.   becomes the current "candidate". After all lines are checked, the
  984.   candidate move is put at the top of the Tree[] array and will be played by
  985.   the program. Note that the program does not handle book transpositions.
  986. */
  987.  
  988. {
  989.   short j, pnt;
  990.   unsigned short m, *mp;
  991.   unsigned r, r0;
  992.   struct BookEntry *p;
  993.  
  994.   srand ((unsigned int) time ((long *) 0));
  995.   r0 = m = 0;
  996.   p = Book;
  997.   while (p != NULL)
  998.     {
  999.       mp = p->mv;
  1000.       for (j = 1; j <= GameCnt; j++)
  1001.     if (GameList[j].gmove != *(mp++))
  1002.       break;
  1003.       if (j > GameCnt)
  1004.     if ((r = urand ()) > r0)
  1005.       {
  1006.         r0 = r;
  1007.         m = *mp;
  1008.         *hint = *(++mp);
  1009.       }
  1010.       p = p->next;
  1011.     }
  1012.  
  1013.   for (pnt = TrPnt[1]; pnt < TrPnt[2]; pnt++)
  1014.     if (((Tree[pnt].f << 8) | Tree[pnt].t) == m)
  1015.       Tree[pnt].score = 0;
  1016.   pick (TrPnt[1], TrPnt[2] - 1);
  1017.   if (Tree[TrPnt[1]].score < 0)
  1018.     Book = NULL;
  1019. }
  1020.  
  1021.  
  1022. inline void
  1023. repetition (short int *cnt)
  1024.  
  1025. /*
  1026.   Check for draw by threefold repetition.
  1027. */
  1028.  
  1029. {
  1030.   register short i, c, f, t;
  1031.   short b[64];
  1032.   unsigned short m;
  1033.  
  1034.   *cnt = c = 0;
  1035.   if (GameCnt > Game50 + 3)
  1036.     {
  1037. #ifdef NOMEMSET
  1038.       for (i = 0; i < 64; b[i++] = 0) ;
  1039. #else
  1040.       memset ((char *) b, 0, sizeof (b));
  1041. #endif /* NOMEMSET */
  1042.       for (i = GameCnt; i > Game50; i--)
  1043.     {
  1044.       m = GameList[i].gmove;
  1045.       f = m >> 8;
  1046.       t = m & 0xFF;
  1047.       if (++b[f] == 0)
  1048.         c--;
  1049.       else
  1050.         c++;
  1051.       if (--b[t] == 0)
  1052.         c--;
  1053.       else
  1054.         c++;
  1055.       if (c == 0)
  1056.         (*cnt)++;
  1057.     }
  1058.     }
  1059. }
  1060.  
  1061. int
  1062. search (short int side,
  1063.     short int ply,
  1064.     short int depth,
  1065.     short int alpha,
  1066.     short int beta,
  1067.     short unsigned int *bstline,
  1068.     short int *rpt)
  1069.  
  1070. /*
  1071.   Perform an alpha-beta search to determine the score for the current board
  1072.   position. If depth <= 0 only capturing moves, pawn promotions and
  1073.   responses to check are generated and searched, otherwise all moves are
  1074.   processed. The search depth is modified for check evasions, certain
  1075.   re-captures and threats. Extensions may continue for up to 11 ply beyond
  1076.   the nominal search depth.
  1077. */
  1078.  
  1079. #define UpdateSearchStatus \
  1080. {\
  1081.    if (flag.post) ShowCurrentMove(pnt,node->f,node->t);\
  1082.      if (pnt > TrPnt[1])\
  1083.        {\
  1084.       d = best-Zscore; e = best-node->score;\
  1085.         if (best < alpha) ExtraTime = 10*ResponseTime;\
  1086.         else if (d > -zwndw && e > 4*zwndw) ExtraTime = -ResponseTime/3;\
  1087.         else if (d > -zwndw) ExtraTime = 0;\
  1088.         else if (d > -3*zwndw) ExtraTime = ResponseTime;\
  1089.         else if (d > -9*zwndw) ExtraTime = 3*ResponseTime;\
  1090.         else ExtraTime = 5*ResponseTime;\
  1091.         }\
  1092.         }
  1093. #define prune (cf && score+node->score < alpha)
  1094. #define ReCapture (flag.rcptr && score > alpha && score < beta &&\
  1095.            ply > 2 && CptrFlag[ply-1] && CptrFlag[ply-2])
  1096. /* && depth == Sdepth-ply+1 */
  1097. #define Parry (hung[side] > 1 && ply == Sdepth+1)
  1098. #define MateThreat (ply < Sdepth+4 && ply > 4 &&\
  1099.             ChkFlag[ply-2] && ChkFlag[ply-4] &&\
  1100.             ChkFlag[ply-2] != ChkFlag[ply-4])
  1101.  
  1102. {
  1103.   register short j, pnt;
  1104.   short best, tempb, tempc, tempsf, tempst;
  1105.   short xside, pbst, d, e, cf, score, rcnt, slk, InChk;
  1106.   unsigned short mv, nxtline[maxdepth];
  1107.   struct leaf *node, tmp;
  1108.  
  1109.   NodeCnt++;
  1110.   xside = otherside[side];
  1111.  
  1112.   if ((ply <= Sdepth + 3) && rpthash[side][hashkey & 0xFF] > 0)
  1113.     repetition (rpt);
  1114.   else
  1115.     *rpt = 0;
  1116.   /* Detect repetitions a bit earlier. SMC. 12/89 */
  1117.   if (*rpt == 1 && ply > 1)
  1118.     return (0);
  1119.   /* if (*rpt >= 2) return(0); */
  1120.  
  1121.   score = evaluate (side, ply, alpha, beta, INCscore, &slk, &InChk);
  1122.   if (score > 9000)
  1123.     {
  1124.       bstline[ply] = 0;
  1125.       return (score);
  1126.     }
  1127.   if (depth > 0)
  1128.     {
  1129.       /* Allow opponent a chance to check again */
  1130.       if (InChk)
  1131.     depth = (depth < 2) ? 2 : depth;
  1132.       else if (PawnThreat[ply - 1] || ReCapture)
  1133.     ++depth;
  1134.     }
  1135.   else
  1136.     {
  1137.       if (score >= alpha &&
  1138.       (InChk || PawnThreat[ply - 1] || Parry))
  1139.     depth = 1;
  1140.       else if (score <= beta && MateThreat)
  1141.     depth = 1;
  1142.     }
  1143.  
  1144. #if ttblsz
  1145.   if (depth > 0 && flag.hash && ply > 1)
  1146.     {
  1147.       if (ProbeTTable (side, depth, &alpha, &beta, &score) == false)
  1148. #ifdef HASHFILE    
  1149.     if (hashfile && (depth > 5) && (GameCnt < 12))
  1150.       ProbeFTable (side, depth, &alpha, &beta, &score);
  1151. #else
  1152.       /* do nothing */;
  1153. #endif /* HASHFILE */      
  1154.       bstline[ply] = PV;
  1155.       bstline[ply + 1] = 0;
  1156.       if (beta == -20000)
  1157.     return (score);
  1158.       if (alpha > beta)
  1159.     return (alpha);
  1160.     }
  1161. #endif /* ttblsz */
  1162.   if (Sdepth == 1)
  1163.     d = 7;
  1164.   else
  1165.     d = 11;
  1166.   if (ply > Sdepth + d || (depth < 1 && score > beta))
  1167.     /* score >= beta ?? */
  1168.     return (score);
  1169.  
  1170.   if (ply > 1)
  1171.     if (depth > 0)
  1172.       MoveList (side, ply);
  1173.     else
  1174.       CaptureList (side, ply);
  1175.  
  1176.   if (TrPnt[ply] == TrPnt[ply + 1])
  1177.     return (score);
  1178.  
  1179.   cf = (depth < 1 && ply > Sdepth + 1 && !ChkFlag[ply - 2] && !slk);
  1180.  
  1181.   if (depth > 0)
  1182.     best = -12000;
  1183.   else
  1184.     best = score;
  1185.   if (best > alpha)
  1186.     alpha = best;
  1187.  
  1188.   for (pnt = pbst = TrPnt[ply];
  1189.        pnt < TrPnt[ply + 1] && best <= beta;    /* best < beta ?? */
  1190.        pnt++)
  1191.     {
  1192.       if (ply > 1)
  1193.     pick (pnt, TrPnt[ply + 1] - 1);
  1194.       node = &Tree[pnt];
  1195.       mv = (node->f << 8) | node->t;
  1196.       nxtline[ply + 1] = 0;
  1197.  
  1198.       if (prune)
  1199.     break;
  1200.       if (ply == 1)
  1201.     UpdateSearchStatus;
  1202.  
  1203.       if (!(node->flags & exact))
  1204.     {
  1205.       MakeMove (side, node, &tempb, &tempc, &tempsf, &tempst, &INCscore);
  1206.       CptrFlag[ply] = (node->flags & capture);
  1207.       PawnThreat[ply] = (node->flags & pwnthrt);
  1208.       Tscore[ply] = node->score;
  1209.       PV = node->reply;
  1210.       node->score = -search (xside, ply + 1,
  1211.                  (depth > 0) ? depth - 1 : 0,
  1212.                  -beta, -alpha,
  1213.                  nxtline, &rcnt);
  1214.       if (abs (node->score) > 9000)
  1215.         node->flags |= exact;
  1216.       else if (rcnt == 1)
  1217.         node->score /= 2;
  1218.       if (rcnt >= 2 || GameCnt - Game50 > 99 ||
  1219.           (node->score == 9999 - ply && !ChkFlag[ply]))
  1220.         {
  1221.           node->flags |= draw;
  1222.           node->flags |= exact;
  1223.           if (side == computer)
  1224.         node->score = contempt;
  1225.           else
  1226.         node->score = -contempt;
  1227.         }
  1228.       node->reply = nxtline[ply + 1];
  1229.       UnmakeMove (side, node, &tempb, &tempc, &tempsf, &tempst);
  1230.     }
  1231.       if (node->score > best && !flag.timeout)
  1232.     {
  1233.       if (depth > 0)
  1234.         if (node->score > alpha && !(node->flags & exact))
  1235.           node->score += depth;
  1236.       best = node->score;
  1237.       pbst = pnt;
  1238.       if (best > alpha)
  1239.         alpha = best;
  1240.       for (j = ply + 1; nxtline[j] > 0; j++)
  1241.         bstline[j] = nxtline[j];
  1242.       bstline[j] = 0;
  1243.       bstline[ply] = mv;
  1244.       if (ply == 1)
  1245.         {
  1246.           if (best > root->score)
  1247.         {
  1248.           tmp = Tree[pnt];
  1249.           for (j = pnt - 1; j >= 0; j--)
  1250.             Tree[j + 1] = Tree[j];
  1251.           Tree[0] = tmp;
  1252.           pbst = 0;
  1253.         }
  1254.           if (Sdepth > 2)
  1255.         if (best > beta)
  1256.           ShowResults (best, bstline, '+');
  1257.         else if (best < alpha)
  1258.           ShowResults (best, bstline, '-');
  1259.         else
  1260.           ShowResults (best, bstline, '&');
  1261.         }
  1262.     }
  1263.       if (NodeCnt > ETnodes)
  1264.     ElapsedTime (0);
  1265.       if (flag.timeout)
  1266.     return (-Tscore[ply - 1]);
  1267.     }
  1268.  
  1269.   node = &Tree[pbst];
  1270.   mv = (node->f << 8) | node->t;
  1271. #if ttblsz
  1272.   if (flag.hash && ply <= Sdepth && *rpt == 0 && best == alpha)
  1273.     {
  1274.       PutInTTable (side, best, depth, alpha, beta, mv);
  1275. #ifdef HASHFILE      
  1276.       if (hashfile && (depth > 5) && (GameCnt < 12))
  1277.     PutInFTable (side, best, depth, alpha, beta, node->f, node->t);
  1278. #endif /* HASHFILE */      
  1279.     }
  1280. #endif /* ttblsz */
  1281.   if (depth > 0)
  1282.     {
  1283.       j = (node->f << 6) | node->t;
  1284.       if (side == black)
  1285.     j |= 0x1000;
  1286.       if (history[j] < 150)
  1287.     history[j] += (unsigned char) 2 * depth;
  1288.       if (node->t != (GameList[GameCnt].gmove & 0xFF))
  1289.     if (best <= beta)
  1290.       killr3[ply] = mv;
  1291.     else if (mv != killr1[ply])
  1292.       {
  1293.         killr2[ply] = killr1[ply];
  1294.         killr1[ply] = mv;
  1295.       }
  1296.       if (best > 9000)
  1297.     killr0[ply] = mv;
  1298.       else
  1299.     killr0[ply] = 0;
  1300.     }
  1301.   return (best);
  1302. }
  1303.  
  1304. #if ttblsz
  1305. /*
  1306.   hashbd contains a 32 bit "signature" of the board position. hashkey
  1307.   contains a 16 bit code used to address the hash table. When a move is
  1308.   made, XOR'ing the hashcode of moved piece on the from and to squares with
  1309.   the hashbd and hashkey values keeps things current.
  1310. */
  1311. #define UpdateHashbd(side, piece, f, t) \
  1312. {\
  1313.   if ((f) >= 0)\
  1314.     {\
  1315.       hashbd ^= hashcode[side][piece][f].bd;\
  1316.       hashkey ^= hashcode[side][piece][f].key;\
  1317.     }\
  1318.   if ((t) >= 0)\
  1319.     {\
  1320.       hashbd ^= hashcode[side][piece][t].bd;\
  1321.       hashkey ^= hashcode[side][piece][t].key;\
  1322.     }\
  1323. }
  1324.  
  1325. #define CB(i) (unsigned char) ((color[2 * (i)] ? 0x80 : 0)\
  1326.            | (board[2 * (i)] << 4)\
  1327.            | (color[2 * (i) + 1] ? 0x8 : 0)\
  1328.            | (board[2 * (i) + 1]))
  1329.  
  1330. int
  1331. ProbeTTable (short int side,
  1332.          short int depth,
  1333.          short int *alpha,
  1334.          short int *beta,
  1335.          short int *score)
  1336.  
  1337. /*
  1338.   Look for the current board position in the transposition table.
  1339. */
  1340.  
  1341. {
  1342.   register struct hashentry *ptbl;
  1343.   register unsigned short i;
  1344.  
  1345.   ptbl = &ttable[side][hashkey & (ttblsz - 1)];
  1346.  
  1347.   /* rehash max rehash times */
  1348.   for (i = 1; ptbl->hashbd != hashbd && i <= rehash; i++)
  1349.     ptbl = &ttable[side][(hashkey + i) & (ttblsz - 1)];
  1350.   if ((short) ptbl->depth >= depth && ptbl->hashbd == hashbd)
  1351.     {
  1352.       HashCnt++;
  1353. #ifdef HASHTEST
  1354.       for (i = 0; i < 32; i++)
  1355.     {
  1356.       if (ptbl->bd[i] != CB(i))
  1357.         {
  1358.           HashCol++;
  1359.           ShowMessage("ttable collision detected");
  1360.           break;
  1361.         }
  1362.     }
  1363. #endif /* HASHTEST */
  1364.  
  1365.       PV = ptbl->mv;
  1366.       if (ptbl->flags & truescore)
  1367.     {
  1368.       *score = ptbl->score;
  1369.       *beta = -20000;
  1370.     }
  1371. #if 0 /* commented out, why? */
  1372.       else if (ptbl->flags & upperbound)
  1373.     {
  1374.       if (ptbl->score < *beta) *beta = ptbl->score+1;
  1375.     }
  1376. #endif
  1377.       else if (ptbl->flags & lowerbound)
  1378.     {
  1379.       if (ptbl->score > *alpha)
  1380.         *alpha = ptbl->score - 1;
  1381.     }
  1382.       return(true);
  1383.     }
  1384.   return(false);
  1385. }
  1386.  
  1387. void
  1388. PutInTTable (short int side,
  1389.          short int score,
  1390.          short int depth,
  1391.          short int alpha,
  1392.          short int beta,
  1393.          short unsigned int mv)
  1394.  
  1395. /*
  1396.   Store the current board position in the transposition table.
  1397. */
  1398.  
  1399. {
  1400.   register struct hashentry *ptbl;
  1401.   register unsigned short i;
  1402.  
  1403.   ptbl = &ttable[side][hashkey & (ttblsz - 1)];
  1404.  
  1405.   /* rehash max rehash times */
  1406.   for (i = 1; depth < ptbl->depth && ptbl->hashbd != hashbd && i <= rehash; i++)
  1407.     ptbl = &ttable[side][(hashkey + i) & (ttblsz - 1)];
  1408.   if (depth >= ptbl->depth || ptbl->hashbd != hashbd)
  1409.     {
  1410.       ptbl->hashbd = hashbd;
  1411.       ptbl->depth = (unsigned char) depth;
  1412.       ptbl->score = score;
  1413.       ptbl->mv = mv;
  1414.       ptbl->flags = 0;
  1415.       if (score < alpha)
  1416.     ptbl->flags |= upperbound;
  1417.       else if (score > beta)
  1418.     ptbl->flags |= lowerbound;
  1419.       else
  1420.     ptbl->flags |= truescore;
  1421. #ifdef HASHTEST
  1422.       for (i = 0; i < 32; i++)
  1423.     {
  1424.       ptbl->bd[i] = CB(i);
  1425.     }
  1426. #endif /* HASHTEST */
  1427.     }
  1428. }
  1429.  
  1430. void
  1431. ZeroTTable (void)
  1432. {
  1433.   register int side, i;
  1434.  
  1435.   if (flag.hash)
  1436.     for (side = white; side <= black; side++)
  1437.       for (i = 0; i < ttblsz; i++)
  1438.     ttable[side][i].depth = 0;
  1439. }
  1440.  
  1441. #ifdef HASHFILE
  1442. int
  1443. ProbeFTable(short int side,
  1444.         short int depth,
  1445.         short int *alpha,
  1446.         short int *beta,
  1447.         short int *score)
  1448.  
  1449. /*
  1450.   Look for the current board position in the persistent transposition table.
  1451. */
  1452.  
  1453. {
  1454.   register unsigned short i, j;
  1455.   register unsigned long hashix;
  1456.   short s;
  1457.   struct fileentry new, t;
  1458.  
  1459.   if (side == white)
  1460.     hashix = hashkey & 0xFFFFFFFE & (filesz - 1);
  1461.   else
  1462.     hashix = hashkey | 1 & (filesz - 1);
  1463.  
  1464.   for (i = 0; i < 32; i++)
  1465.     new.bd[i] = CB(i);
  1466.   new.flags = 0;
  1467.   if ((Mvboard[kingP[side]] == 0) && (Mvboard[qrook[side]] == 0))
  1468.     new.flags |= queencastle;
  1469.   if ((Mvboard[kingP[side]] == 0) && (Mvboard[krook[side]] == 0))
  1470.     new.flags |= kingcastle;
  1471.  
  1472.   for (i = 0; i < frehash; i++)
  1473.     {
  1474.       fseek(hashfile,
  1475.         sizeof(struct fileentry) * ((hashix + 2 * i) & (filesz - 1)),
  1476.         SEEK_SET);
  1477.       fread(&t, sizeof(struct fileentry), 1, hashfile);
  1478.       for (j = 0; j < 32; j++)
  1479.     if (t.bd[j] != new.bd[j])
  1480.       break;
  1481.       if (((short) t.depth >= depth) && (j >= 32)
  1482.       && (new.flags == (t.flags & (kingcastle | queencastle))))
  1483.     {
  1484.       FHashCnt++;
  1485.       PV = (t.f << 8) | t.t;
  1486.       s = (t.sh << 8) | t.sl;
  1487.       if (t.flags & truescore)
  1488.         {
  1489.           *score = s;
  1490.           *beta = -20000;
  1491.         }
  1492.       else if (t.flags & lowerbound)
  1493.         {
  1494.           if (s > *alpha)
  1495.         *alpha = s - 1;
  1496.         }
  1497.       return(true);
  1498.     }
  1499.     }
  1500.   return(false);
  1501. }
  1502.  
  1503. void
  1504. PutInFTable (short int side,
  1505.          short int score,
  1506.          short int depth,
  1507.          short int alpha,
  1508.          short int beta,
  1509.          short unsigned int f,
  1510.          short unsigned int t)
  1511.  
  1512. /*
  1513.   Store the current board position in the persistent transposition table.
  1514. */
  1515.  
  1516. {
  1517.   register unsigned short i;
  1518.   register unsigned long hashix;
  1519.   struct fileentry new, tmp;
  1520.  
  1521.   if (side == white)
  1522.     hashix = hashkey & 0xFFFFFFFE & (filesz - 1);
  1523.   else
  1524.     hashix = hashkey | 1 & (filesz - 1);
  1525.  
  1526.   for (i = 0; i < 32; i++)
  1527.     new.bd[i] = CB(i);
  1528.   new.f = (unsigned char) f;
  1529.   new.t = (unsigned char) t;
  1530.   new.flags = 0;
  1531.   if (score < alpha)
  1532.     new.flags |= upperbound;
  1533.   else if (score > beta)
  1534.     new.flags |= lowerbound;
  1535.   else
  1536.     new.flags |= truescore;
  1537.   if ((Mvboard[kingP[side]] == 0) && (Mvboard[qrook[side]] == 0))
  1538.     new.flags |= queencastle;
  1539.   if ((Mvboard[kingP[side]] == 0) && (Mvboard[krook[side]] == 0))
  1540.     new.flags |= kingcastle;
  1541.   new.depth = (unsigned char) depth;
  1542.   new.sh = (unsigned char) (score >> 8);
  1543.   new.sl = (unsigned char) (score & 0xFF);
  1544.  
  1545.   for (i = 0; i < frehash; i++)
  1546.     {
  1547.       fseek(hashfile,
  1548.         sizeof(struct fileentry) * ((hashix + 2 * i) & (filesz - 1)),
  1549.         SEEK_SET);
  1550.       fread(&tmp, sizeof(struct fileentry), 1, hashfile);
  1551.       if ((short) tmp.depth <= depth)
  1552.     {
  1553.       fseek(hashfile,
  1554.         sizeof(struct fileentry) * ((hashix + 2 * i) & (filesz - 1)),
  1555.         SEEK_SET);
  1556.       fwrite (&new, sizeof(struct fileentry), 1, hashfile);
  1557.       break;
  1558.     }
  1559.     }
  1560. }
  1561. #endif /* HASHFILE */
  1562. #endif /* ttblsz */
  1563.  
  1564. void
  1565. ZeroRPT (void)
  1566. {
  1567.   register int side, i;
  1568.  
  1569.   for (side = white; side <= black; side++)
  1570.     for (i = 0; i < 256; i++)
  1571.       rpthash[side][i] = 0;
  1572. }
  1573.  
  1574. #define Link(from,to,flag,s) \
  1575. {\
  1576.    node->f = from; node->t = to;\
  1577.      node->reply = 0;\
  1578.        node->flags = flag;\
  1579.      node->score = s;\
  1580.        ++node;\
  1581.          ++TrPnt[ply+1];\
  1582.          }
  1583.  
  1584. static inline void
  1585. LinkMove (short int ply,
  1586.       short int f,
  1587.       short int t,
  1588.       short int flag,
  1589.       short int xside)
  1590.  
  1591. /*
  1592.   Add a move to the tree.  Assign a bonus to order the moves
  1593.   as follows:
  1594.   1. Principle variation
  1595.   2. Capture of last moved piece
  1596.   3. Other captures (major pieces first)
  1597.   4. Killer moves
  1598.   5. "history" killers
  1599. */
  1600.  
  1601. {
  1602.   register short s, z;
  1603.   register unsigned short mv;
  1604.   register struct leaf *node;
  1605.  
  1606.   node = &Tree[TrPnt[ply + 1]];
  1607.   mv = (f << 8) | t;
  1608.   s = 0;
  1609.   if (mv == Swag0)
  1610.     s = 2000;
  1611.   else if (mv == Swag1)
  1612.     s = 60;
  1613.   else if (mv == Swag2)
  1614.     s = 50;
  1615.   else if (mv == Swag3)
  1616.     s = 40;
  1617.   else if (mv == Swag4)
  1618.     s = 30;
  1619.   z = (f << 6) | t;
  1620.   if (xside == white)
  1621.     z |= 0x1000;
  1622.   s += history[z];
  1623.   if (color[t] != neutral)
  1624.     {
  1625.       if (t == TOsquare)
  1626.     s += 500;
  1627.       s += value[board[t]] - board[f];
  1628.     }
  1629.   if (board[f] == pawn)
  1630.     if (row (t) == 0 || row (t) == 7)
  1631.       {
  1632.     flag |= promote;
  1633.     s += 800;
  1634.     Link (f, t, flag | queen, s - 20000);
  1635.     s -= 200;
  1636.     Link (f, t, flag | knight, s - 20000);
  1637.     s -= 50;
  1638.     Link (f, t, flag | rook, s - 20000);
  1639.     flag |= bishop;
  1640.     s -= 50;
  1641.       }
  1642.     else if (row (t) == 1 || row (t) == 6)
  1643.       {
  1644.     flag |= pwnthrt;
  1645.     s += 600;
  1646.       }
  1647.   Link (f, t, flag, s - 20000);
  1648. }
  1649.  
  1650.  
  1651. static inline void
  1652. GenMoves (short int ply, short int sq, short int side, short int xside)
  1653.  
  1654. /*
  1655.   Generate moves for a piece. The moves are taken from the precalulated
  1656.   array nextpos/nextdir. If the board is free, next move is choosen from
  1657.   nextpos else from nextdir.
  1658. */
  1659.  
  1660. {
  1661.   register short u, piece;
  1662.   register unsigned char *ppos, *pdir;
  1663.  
  1664.   piece = board[sq];
  1665.   ppos = nextpos[ptype[side][piece]][sq];
  1666.   pdir = nextdir[ptype[side][piece]][sq];
  1667.   if (piece == pawn)
  1668.     {
  1669.       u = ppos[sq];    /* follow no captures thread */
  1670.       if (color[u] == neutral)
  1671.     {
  1672.       LinkMove (ply, sq, u, 0, xside);
  1673.       u = ppos[u];
  1674.       if (color[u] == neutral)
  1675.         LinkMove (ply, sq, u, 0, xside);
  1676.     }
  1677.       u = pdir[sq];    /* follow captures thread */
  1678.       if (color[u] == xside)
  1679.     LinkMove (ply, sq, u, capture, xside);
  1680.       else
  1681.     if (u == epsquare)
  1682.       LinkMove (ply, sq, u, capture | epmask, xside);
  1683.       u = pdir[u];
  1684.       if (color[u] == xside)
  1685.     LinkMove (ply, sq, u, capture, xside);
  1686.       else
  1687.     if (u == epsquare)
  1688.       LinkMove (ply, sq, u, capture | epmask, xside);
  1689.     }
  1690.   else
  1691.     {
  1692.       u = ppos[sq];
  1693.       do
  1694.     {
  1695.       if (color[u] == neutral)
  1696.         {
  1697.           LinkMove (ply, sq, u, 0, xside);
  1698.           u = ppos[u];
  1699.         }
  1700.       else
  1701.         {
  1702.           if (color[u] == xside)
  1703.         LinkMove (ply, sq, u, capture, xside);
  1704.           u = pdir[u];
  1705.         }
  1706.       } while (u != sq);
  1707.     }
  1708. }
  1709.  
  1710. void
  1711. MoveList (short int side, short int ply)
  1712.  
  1713. /*
  1714.   Fill the array Tree[] with all available moves for side to play. Array
  1715.   TrPnt[ply] contains the index into Tree[] of the first move at a ply.
  1716. */
  1717.  
  1718. {
  1719.   register short i, xside, f;
  1720.  
  1721.   xside = otherside[side];
  1722.   TrPnt[ply + 1] = TrPnt[ply];
  1723.   if (PV == 0)
  1724.     Swag0 = killr0[ply];
  1725.   else
  1726.     Swag0 = PV;
  1727.   Swag1 = killr1[ply];
  1728.   Swag2 = killr2[ply];
  1729.   Swag3 = killr3[ply];
  1730.   if (ply > 2)
  1731.     Swag4 = killr1[ply - 2];
  1732.   else
  1733.     Swag4 = 0;
  1734.   for (i = PieceCnt[side]; i >= 0; i--)
  1735.     GenMoves (ply, PieceList[side][i], side, xside);
  1736.   if (!castld[side])
  1737.     {
  1738.       f = PieceList[side][0];
  1739.       if (castle (side, f, f + 2, 0))
  1740.     {
  1741.       LinkMove (ply, f, f + 2, cstlmask, xside);
  1742.     }
  1743.       if (castle (side, f, f - 2, 0))
  1744.     {
  1745.       LinkMove (ply, f, f - 2, cstlmask, xside);
  1746.     }
  1747.     }
  1748. }
  1749.  
  1750. void
  1751. CaptureList (short int side, short int ply)
  1752.  
  1753. /*
  1754.   Fill the array Tree[] with all available cature and promote moves for
  1755.   side to play. Array TrPnt[ply] contains the index into Tree[]
  1756.   of the first move at a ply.
  1757. */
  1758.  
  1759. {
  1760.   register short u, sq, xside;
  1761.   register struct leaf *node;
  1762.   register unsigned char *ppos, *pdir;
  1763.   short i, piece, *PL, r7;
  1764.  
  1765.   xside = otherside[side];
  1766.   TrPnt[ply + 1] = TrPnt[ply];
  1767.   node = &Tree[TrPnt[ply]];
  1768.   r7 = rank7[side];
  1769.   PL = PieceList[side];
  1770.   for (i = 0; i <= PieceCnt[side]; i++)
  1771.     {
  1772.       sq = PL[i];
  1773.       piece = board[sq];
  1774.       if (sweep[piece])
  1775.     {
  1776.       ppos = nextpos[piece][sq];
  1777.       pdir = nextdir[piece][sq];
  1778.       u = ppos[sq];
  1779.       do
  1780.         {
  1781.           if (color[u] == neutral)
  1782.         u = ppos[u];
  1783.           else
  1784.         {
  1785.           if (color[u] == xside)
  1786.             Link (sq, u, capture,
  1787.               value[board[u]] + svalue[board[u]] - piece);
  1788.           u = pdir[u];
  1789.         }
  1790.       } while (u != sq);
  1791.     }
  1792.       else
  1793.     {
  1794.       pdir = nextdir[ptype[side][piece]][sq];
  1795.       if (piece == pawn && row (sq) == r7)
  1796.         {
  1797.           u = pdir[sq];
  1798.           if (color[u] == xside)
  1799.         Link (sq, u, capture | promote | queen, valueQ);
  1800.           u = pdir[u];
  1801.           if (color[u] == xside)
  1802.         Link (sq, u, capture | promote | queen, valueQ);
  1803.           ppos = nextpos[ptype[side][piece]][sq];
  1804.           u = ppos[sq]; /* also generate non capture promote */
  1805.           if (color[u] == neutral)
  1806.         Link (sq, u, promote | queen, valueQ);
  1807.         }
  1808.       else
  1809.         {
  1810.           u = pdir[sq];
  1811.           do
  1812.         {
  1813.           if (color[u] == xside)
  1814.             Link (sq, u, capture,
  1815.               value[board[u]] + svalue[board[u]] - piece);
  1816.           u = pdir[u];
  1817.           } while (u != sq);
  1818.         }
  1819.     }
  1820.     }
  1821. }
  1822.  
  1823.  
  1824. int
  1825. castle (short int side, short int kf, short int kt, short int iop)
  1826.  
  1827. /* Make or Unmake a castling move. */
  1828.  
  1829. {
  1830.   register short rf, rt, t0, xside;
  1831.  
  1832.   xside = otherside[side];
  1833.   if (kt > kf)
  1834.     {
  1835.       rf = kf + 3;
  1836.       rt = kt - 1;
  1837.     }
  1838.   else
  1839.     {
  1840.       rf = kf - 4;
  1841.       rt = kt + 1;
  1842.     }
  1843.   if (iop == 0)
  1844.     {
  1845.       if (kf != kingP[side] ||
  1846.       board[kf] != king ||
  1847.       board[rf] != rook ||
  1848.       Mvboard[kf] != 0 ||
  1849.       Mvboard[rf] != 0 ||
  1850.       color[kt] != neutral ||
  1851.       color[rt] != neutral ||
  1852.       color[kt - 1] != neutral ||
  1853.       SqAtakd (kf, xside) ||
  1854.       SqAtakd (kt, xside) ||
  1855.       SqAtakd (rt, xside))
  1856.     return (false);
  1857.     }
  1858.   else
  1859.     {
  1860.       if (iop == 1)
  1861.     {
  1862.       castld[side] = true;
  1863.       Mvboard[kf]++;
  1864.       Mvboard[rf]++;
  1865.     }
  1866.       else
  1867.     {
  1868.       castld[side] = false;
  1869.       Mvboard[kf]--;
  1870.       Mvboard[rf]--;
  1871.       t0 = kt;
  1872.       kt = kf;
  1873.       kf = t0;
  1874.       t0 = rt;
  1875.       rt = rf;
  1876.       rf = t0;
  1877.     }
  1878.       board[kt] = king;
  1879.       color[kt] = side;
  1880.       Pindex[kt] = 0;
  1881.       board[kf] = no_piece;
  1882.       color[kf] = neutral;
  1883.       board[rt] = rook;
  1884.       color[rt] = side;
  1885.       Pindex[rt] = Pindex[rf];
  1886.       board[rf] = no_piece;
  1887.       color[rf] = neutral;
  1888.       PieceList[side][Pindex[kt]] = kt;
  1889.       PieceList[side][Pindex[rt]] = rt;
  1890. #if ttblsz
  1891.       UpdateHashbd (side, king, kf, kt);
  1892.       UpdateHashbd (side, rook, rf, rt);
  1893. #endif /* ttblsz */
  1894.     }
  1895.   return (true);
  1896. }
  1897.  
  1898.  
  1899. static inline void
  1900. EnPassant (short int xside, short int f, short int t, short int iop)
  1901.  
  1902. /*
  1903.   Make or unmake an en passant move.
  1904. */
  1905.  
  1906. {
  1907.   register short l;
  1908.  
  1909.   if (t > f)
  1910.     l = t - 8;
  1911.   else
  1912.     l = t + 8;
  1913.   if (iop == 1)
  1914.     {
  1915.       board[l] = no_piece;
  1916.       color[l] = neutral;
  1917.     }
  1918.   else
  1919.     {
  1920.       board[l] = pawn;
  1921.       color[l] = xside;
  1922.     }
  1923.   InitializeStats ();
  1924. }
  1925.  
  1926.  
  1927. static inline void
  1928. UpdatePieceList (short int side, short int sq, short int iop)
  1929.  
  1930. /*
  1931.   Update the PieceList and Pindex arrays when a piece is captured or when a
  1932.   capture is unmade.
  1933. */
  1934.  
  1935. {
  1936.   register short i;
  1937.   if (iop == 1)
  1938.     {
  1939.       PieceCnt[side]--;
  1940.       for (i = Pindex[sq]; i <= PieceCnt[side]; i++)
  1941.     {
  1942.       PieceList[side][i] = PieceList[side][i + 1];
  1943.       Pindex[PieceList[side][i]] = i;
  1944.     }
  1945.     }
  1946.   else
  1947.     {
  1948.       PieceCnt[side]++;
  1949.       PieceList[side][PieceCnt[side]] = sq;
  1950.       Pindex[sq] = PieceCnt[side];
  1951.     }
  1952. }
  1953.  
  1954. void
  1955. MakeMove (short int side,
  1956.       struct leaf * node,
  1957.       short int *tempb,
  1958.       short int *tempc,
  1959.       short int *tempsf,
  1960.       short int *tempst,
  1961.       short int *INCscore)
  1962.  
  1963. /*
  1964.   Update Arrays board[], color[], and Pindex[] to reflect the new board
  1965.   position obtained after making the move pointed to by node. Also update
  1966.   miscellaneous stuff that changes when a move is made.
  1967. */
  1968.  
  1969. {
  1970.   register short f, t, xside, ct, cf;
  1971.  
  1972.   xside = otherside[side];
  1973.   GameCnt++;
  1974.   f = node->f;
  1975.   t = node->t;
  1976.   epsquare = -1;
  1977.   FROMsquare = f;
  1978.   TOsquare = t;
  1979.   *INCscore = 0;
  1980.   GameList[GameCnt].gmove = (f << 8) | t;
  1981.   if (node->flags & cstlmask)
  1982.     {
  1983.       GameList[GameCnt].piece = no_piece;
  1984.       GameList[GameCnt].color = side;
  1985.       (void) castle (side, f, t, 1);
  1986.     }
  1987.   else
  1988.     {
  1989.       if (!(node->flags & capture) && (board[f] != pawn))
  1990.         rpthash[side][hashkey & 0xFF]++;
  1991.       *tempc = color[t];
  1992.       *tempb = board[t];
  1993.       *tempsf = svalue[f];
  1994.       *tempst = svalue[t];
  1995.       GameList[GameCnt].piece = *tempb;
  1996.       GameList[GameCnt].color = *tempc;
  1997.       if (*tempc != neutral)
  1998.     {
  1999.       UpdatePieceList (*tempc, t, 1);
  2000.       if (*tempb == pawn)
  2001.         --PawnCnt[*tempc][column (t)];
  2002.       if (board[f] == pawn)
  2003.         {
  2004.           --PawnCnt[side][column (f)];
  2005.           ++PawnCnt[side][column (t)];
  2006.           cf = column (f);
  2007.           ct = column (t);
  2008.           if (PawnCnt[side][ct] > 1 + PawnCnt[side][cf])
  2009.         *INCscore -= 15;
  2010.           else if (PawnCnt[side][ct] < 1 + PawnCnt[side][cf])
  2011.         *INCscore += 15;
  2012.           else if (ct == 0 || ct == 7 || PawnCnt[side][ct + ct - cf] == 0)
  2013.         *INCscore -= 15;
  2014.         }
  2015.       mtl[xside] -= value[*tempb];
  2016.       if (*tempb == pawn)
  2017.         pmtl[xside] -= valueP;
  2018. #if ttblsz
  2019.       UpdateHashbd (xside, *tempb, -1, t);
  2020. #endif /* ttblsz */
  2021.       *INCscore += *tempst;
  2022.       Mvboard[t]++;
  2023.     }
  2024.       color[t] = color[f];
  2025.       board[t] = board[f];
  2026.       svalue[t] = svalue[f];
  2027.       Pindex[t] = Pindex[f];
  2028.       PieceList[side][Pindex[t]] = t;
  2029.       color[f] = neutral;
  2030.       board[f] = no_piece;
  2031.       if (board[t] == pawn)
  2032.     if (t - f == 16)
  2033.       epsquare = f + 8;
  2034.     else if (f - t == 16)
  2035.       epsquare = f - 8;
  2036.       if (node->flags & promote)
  2037.     {
  2038.       board[t] = node->flags & pmask;
  2039.       if (board[t] == queen)
  2040.         HasQueen[side]++;
  2041.       else if (board[t] == rook)
  2042.         HasRook[side]++;
  2043.       else if (board[t] == bishop)
  2044.         HasBishop[side]++;
  2045.       else if (board[t] == knight)
  2046.         HasKnight[side]++;
  2047.       --PawnCnt[side][column (t)];
  2048.       mtl[side] += value[board[t]] - valueP;
  2049.       pmtl[side] -= valueP;
  2050. #if ttblsz
  2051.       UpdateHashbd (side, pawn, f, -1);
  2052.       UpdateHashbd (side, board[t], f, -1);
  2053. #endif /* ttblsz */
  2054.       *INCscore -= *tempsf;
  2055.     }
  2056.       if (node->flags & epmask)
  2057.     EnPassant (xside, f, t, 1);
  2058.       else
  2059. #if ttblsz
  2060.     UpdateHashbd (side, board[t], f, t);
  2061. #else
  2062.     /* NOOP */;        
  2063. #endif /* ttblsz */
  2064.       Mvboard[f]++;
  2065.     }
  2066. }
  2067.  
  2068. void
  2069. UnmakeMove (short int side,
  2070.         struct leaf * node,
  2071.         short int *tempb,
  2072.         short int *tempc,
  2073.         short int *tempsf,
  2074.         short int *tempst)
  2075.  
  2076. /*
  2077.   Take back a move.
  2078. */
  2079.  
  2080. {
  2081.   register short f, t, xside;
  2082.  
  2083.   xside = otherside[side];
  2084.   f = node->f;
  2085.   t = node->t;
  2086.   epsquare = -1;
  2087.   GameCnt--;
  2088.   if (node->flags & cstlmask)
  2089.     (void) castle (side, f, t, 2);
  2090.   else
  2091.     {
  2092.       color[f] = color[t];
  2093.       board[f] = board[t];
  2094.       svalue[f] = *tempsf;
  2095.       Pindex[f] = Pindex[t];
  2096.       PieceList[side][Pindex[f]] = f;
  2097.       color[t] = *tempc;
  2098.       board[t] = *tempb;
  2099.       svalue[t] = *tempst;
  2100.       if (node->flags & promote)
  2101.     {
  2102.       board[f] = pawn;
  2103.       ++PawnCnt[side][column (t)];
  2104.       mtl[side] += valueP - value[node->flags & pmask];
  2105.       pmtl[side] += valueP;
  2106. #if ttblsz
  2107.       UpdateHashbd (side, (short) node->flags & pmask, -1, t);
  2108.       UpdateHashbd (side, pawn, -1, t);
  2109. #endif /* ttblsz */
  2110.     }
  2111.       if (*tempc != neutral)
  2112.     {
  2113.       UpdatePieceList (*tempc, t, 2);
  2114.       if (*tempb == pawn)
  2115.         ++PawnCnt[*tempc][column (t)];
  2116.       if (board[f] == pawn)
  2117.         {
  2118.           --PawnCnt[side][column (t)];
  2119.           ++PawnCnt[side][column (f)];
  2120.         }
  2121.       mtl[xside] += value[*tempb];
  2122.       if (*tempb == pawn)
  2123.         pmtl[xside] += valueP;
  2124. #if ttblsz
  2125.       UpdateHashbd (xside, *tempb, -1, t);
  2126. #endif /* ttblsz */
  2127.       Mvboard[t]--;
  2128.     }
  2129.       if (node->flags & epmask)
  2130.     EnPassant (xside, f, t, 2);
  2131.       else
  2132. #if ttblsz
  2133.     UpdateHashbd (side, board[f], f, t);
  2134. #else
  2135.       /* NOOP */;
  2136. #endif /* ttblsz */
  2137.       Mvboard[f]--;
  2138.       if (!(node->flags & capture) && (board[f] != pawn))
  2139.         rpthash[side][hashkey & 0xFF]--;
  2140.     }
  2141. }
  2142.  
  2143.  
  2144. void
  2145. InitializeStats (void)
  2146.  
  2147. /*
  2148.   Scan thru the board seeing what's on each square. If a piece is found,
  2149.   update the variables PieceCnt, PawnCnt, Pindex and PieceList. Also
  2150.   determine the material for each side and set the hashkey and hashbd
  2151.   variables to represent the current board position. Array
  2152.   PieceList[side][indx] contains the location of all the pieces of either
  2153.   side. Array Pindex[sq] contains the indx into PieceList for a given
  2154.   square.
  2155. */
  2156.  
  2157. {
  2158.   register short i, sq;
  2159.   epsquare = -1;
  2160.   for (i = 0; i < 8; i++)
  2161.     PawnCnt[white][i] = PawnCnt[black][i] = 0;
  2162.   mtl[white] = mtl[black] = pmtl[white] = pmtl[black] = 0;
  2163.   PieceCnt[white] = PieceCnt[black] = 0;
  2164. #if ttblsz
  2165.   hashbd = hashkey = 0;
  2166. #endif /* ttblsz */
  2167.   for (sq = 0; sq < 64; sq++)
  2168.     if (color[sq] != neutral)
  2169.       {
  2170.     mtl[color[sq]] += value[board[sq]];
  2171.     if (board[sq] == pawn)
  2172.       {
  2173.         pmtl[color[sq]] += valueP;
  2174.         ++PawnCnt[color[sq]][column (sq)];
  2175.       }
  2176.     if (board[sq] == king)
  2177.       Pindex[sq] = 0;
  2178.     else
  2179.       Pindex[sq] = ++PieceCnt[color[sq]];
  2180.     PieceList[color[sq]][Pindex[sq]] = sq;
  2181. #if ttblsz
  2182.     hashbd ^= hashcode[color[sq]][board[sq]][sq].bd;
  2183.     hashkey ^= hashcode[color[sq]][board[sq]][sq].key;
  2184. #endif /* ttblsz */
  2185.       }
  2186. }
  2187.  
  2188.  
  2189. int
  2190. SqAtakd (short int sq, short int side)
  2191.  
  2192. /*
  2193.   See if any piece with color 'side' ataks sq.  First check pawns then Queen,
  2194.   Bishop, Rook and King and last Knight.
  2195. */
  2196.  
  2197. {
  2198.   register short u;
  2199.   register unsigned char *ppos, *pdir;
  2200.   short xside;
  2201.  
  2202.   xside = otherside[side];
  2203.   pdir = nextdir[ptype[xside][pawn]][sq];
  2204.   u = pdir[sq];        /* follow captures thread */
  2205.   if (u != sq)
  2206.     {
  2207.       if (board[u] == pawn && color[u] == side)
  2208.     return (true);
  2209.       u = pdir[u];
  2210.       if (u != sq && board[u] == pawn && color[u] == side)
  2211.     return (true);
  2212.     }
  2213.   /* king capture */
  2214.   if (distance (sq, PieceList[side][0]) == 1)
  2215.     return (true);
  2216.   /* try a queen bishop capture */
  2217.   ppos = nextpos[bishop][sq];
  2218.   pdir = nextdir[bishop][sq];
  2219.   u = ppos[sq];
  2220.   do
  2221.     {
  2222.       if (color[u] == neutral)
  2223.     u = ppos[u];
  2224.       else
  2225.     {
  2226.       if (color[u] == side &&
  2227.           (board[u] == queen || board[u] == bishop))
  2228.         return (true);
  2229.       u = pdir[u];
  2230.     }
  2231.   } while (u != sq);
  2232.   /* try a queen rook capture */
  2233.   ppos = nextpos[rook][sq];
  2234.   pdir = nextdir[rook][sq];
  2235.   u = ppos[sq];
  2236.   do
  2237.     {
  2238.       if (color[u] == neutral)
  2239.     u = ppos[u];
  2240.       else
  2241.     {
  2242.       if (color[u] == side &&
  2243.           (board[u] == queen || board[u] == rook))
  2244.         return (true);
  2245.       u = pdir[u];
  2246.     }
  2247.   } while (u != sq);
  2248.   /* try a knight capture */
  2249.   pdir = nextdir[knight][sq];
  2250.   u = pdir[sq];
  2251.   do
  2252.     {
  2253.       if (color[u] == side && board[u] == knight)
  2254.     return (true);
  2255.       u = pdir[u];
  2256.   } while (u != sq);
  2257.   return (false);
  2258. }
  2259.  
  2260. static inline void
  2261. ataks (short int side, short int *a)
  2262.  
  2263. /*
  2264.   Fill array atak[][] with info about ataks to a square.  Bits 8-15 are set
  2265.   if the piece (king..pawn) ataks the square.  Bits 0-7 contain a count of
  2266.   total ataks to the square.
  2267. */
  2268.  
  2269. {
  2270.   register short u, c, sq;
  2271.   register unsigned char *ppos, *pdir;
  2272.   short i, piece, *PL;
  2273.  
  2274. #ifdef NOMEMSET
  2275.   for (u = 64; u; a[--u] = 0) ;
  2276. #else
  2277.   memset ((char *) a, 0, 64 * sizeof (a[0]));
  2278. #endif /* NOMEMSET */
  2279.   PL = PieceList[side];
  2280.   for (i = PieceCnt[side]; i >= 0; i--)
  2281.     {
  2282.       sq = PL[i];
  2283.       piece = board[sq];
  2284.       c = control[piece];
  2285.       if (sweep[piece])
  2286.     {
  2287.       ppos = nextpos[piece][sq];
  2288.       pdir = nextdir[piece][sq];
  2289.       u = ppos[sq];
  2290.       do
  2291.         {
  2292.           a[u] = ++a[u] | c;
  2293.           u = (color[u] == neutral) ? ppos[u] : pdir[u];
  2294.       } while (u != sq);
  2295.     }
  2296.       else
  2297.     {
  2298.       pdir = nextdir[ptype[side][piece]][sq];
  2299.       u = pdir[sq];    /* follow captures thread for pawns */
  2300.       do
  2301.         {
  2302.           a[u] = ++a[u] | c;
  2303.           u = pdir[u];
  2304.       } while (u != sq);
  2305.     }
  2306.     }
  2307. }
  2308.  
  2309.  
  2310. /* ............    POSITIONAL EVALUATION ROUTINES    ............ */
  2311.  
  2312. int
  2313. evaluate (short int side,
  2314.       short int ply,
  2315.       short int alpha,
  2316.       short int beta,
  2317.       short int INCscore,
  2318.       short int *slk,
  2319.       short int *InChk)
  2320.  
  2321. /*
  2322.   Compute an estimate of the score by adding the positional score from the
  2323.   previous ply to the material difference. If this score falls inside a
  2324.   window which is 180 points wider than the alpha-beta window (or within a
  2325.   50 point window during quiescence search) call ScorePosition() to
  2326.   determine a score, otherwise return the estimated score. If one side has
  2327.   only a king and the other either has no pawns or no pieces then the
  2328.   function ScoreLoneKing() is called.
  2329. */
  2330.  
  2331. {
  2332.   register short evflag, xside;
  2333.   short s;
  2334.  
  2335.   xside = otherside[side];
  2336.   s = -Pscore[ply - 1] + mtl[side] - mtl[xside] - INCscore;
  2337.   hung[white] = hung[black] = 0;
  2338.   *slk = ((mtl[white] == valueK && (pmtl[black] == 0 || emtl[black] == 0)) ||
  2339.      (mtl[black] == valueK && (pmtl[white] == 0 || emtl[white] == 0)));
  2340.  
  2341.   if (*slk)
  2342.     evflag = false;
  2343.   else
  2344.     evflag =
  2345.       (ply == 1 || ply < Sdepth ||
  2346.        ((ply == Sdepth + 1 || ply == Sdepth + 2) &&
  2347.     (s > alpha - xwndw && s < beta + xwndw)) ||
  2348.        (ply > Sdepth + 2 && s >= alpha - 25 && s <= beta + 25));
  2349.  
  2350.   if (evflag)
  2351.     {
  2352.       EvalNodes++;
  2353.       ataks (side, atak[side]);
  2354.       if (Anyatak (side, PieceList[xside][0]))
  2355.     return (10001 - ply);
  2356.       ataks (xside, atak[xside]);
  2357.       *InChk = Anyatak (xside, PieceList[side][0]);
  2358.       ScorePosition (side, &s);
  2359.     }
  2360.   else
  2361.     {
  2362.       if (SqAtakd (PieceList[xside][0], side))
  2363.     return (10001 - ply);
  2364.       *InChk = SqAtakd (PieceList[side][0], xside);
  2365.       if (*slk)
  2366.     ScoreLoneKing (side, &s);
  2367.     }
  2368.  
  2369.   Pscore[ply] = s - mtl[side] + mtl[xside];
  2370.   if (*InChk)
  2371.     ChkFlag[ply - 1] = Pindex[TOsquare];
  2372.   else
  2373.     ChkFlag[ply - 1] = 0;
  2374.   return (s);
  2375. }
  2376.  
  2377.  
  2378. static inline int
  2379. ScoreKPK (short int side,
  2380.       short int winner,
  2381.       short int loser,
  2382.       short int king1,
  2383.       short int king2,
  2384.       short int sq)
  2385.  
  2386. /*
  2387.   Score King and Pawns versus King endings.
  2388. */
  2389.  
  2390. {
  2391.   register short s, r;
  2392.  
  2393.   if (PieceCnt[winner] == 1)
  2394.     s = 50;
  2395.   else
  2396.     s = 120;
  2397.   if (winner == white)
  2398.     {
  2399.       if (side == loser)
  2400.     r = row (sq) - 1;
  2401.       else
  2402.     r = row (sq);
  2403.       if (row (king2) >= r && distance (sq, king2) < 8 - r)
  2404.     s += 10 * row (sq);
  2405.       else
  2406.     s = 500 + 50 * row (sq);
  2407.       if (row (sq) < 6)
  2408.     sq += 16;
  2409.       else
  2410.     if (row(sq) == 6)
  2411.       sq += 8;
  2412.     }
  2413.   else
  2414.     {
  2415.       if (side == loser)
  2416.     r = row (sq) + 1;
  2417.       else
  2418.     r = row (sq);
  2419.       if (row (king2) <= r && distance (sq, king2) < r + 1)
  2420.     s += 10 * (7 - row (sq));
  2421.       else
  2422.     s = 500 + 50 * (7 - row (sq));
  2423.       if (row (sq) > 1)
  2424.     sq -= 16;
  2425.       else
  2426.     if (row(sq) == 1)
  2427.       sq -= 8;
  2428.     }
  2429.   s += 8 * (taxicab (king2, sq) - taxicab (king1, sq));
  2430.   return (s);
  2431. }
  2432.  
  2433.  
  2434. static inline int
  2435. ScoreKBNK (short int winner, short int king1, short int king2)
  2436.  
  2437.  
  2438. /*
  2439.   Score King+Bishop+Knight versus King endings.
  2440.   This doesn't work all that well but it's better than nothing.
  2441. */
  2442.  
  2443. {
  2444.   register short s, sq, KBNKsq = 0;
  2445.  
  2446.   for (sq = 0; sq < 64; sq++)
  2447.     if (board[sq] == bishop)
  2448.       if (row (sq) % 2 == column (sq) % 2)
  2449.     KBNKsq = 0;
  2450.       else
  2451.     KBNKsq = 7;
  2452.  
  2453.   s = emtl[winner] - 300;
  2454.   if (KBNKsq == 0)
  2455.     s += KBNK[king2];
  2456.   else
  2457.     s += KBNK[locn (row (king2), 7 - column (king2))];
  2458.   s -= taxicab (king1, king2);
  2459.   s -= distance (PieceList[winner][1], king2);
  2460.   s -= distance (PieceList[winner][2], king2);
  2461.   return (s);
  2462. }
  2463.  
  2464.  
  2465. void
  2466. ScoreLoneKing (short int side, short int *score)
  2467.  
  2468. /*
  2469.   Static evaluation when loser has only a king and winner has no pawns or no
  2470.   pieces.
  2471. */
  2472.  
  2473. {
  2474.   register short winner, loser, king1, king2, s, i;
  2475.  
  2476.   UpdateWeights ();
  2477.   if (mtl[white] > mtl[black])
  2478.     winner = white;
  2479.   else
  2480.     winner = black;
  2481.   loser = otherside[winner];
  2482.   king1 = PieceList[winner][0];
  2483.   king2 = PieceList[loser][0];
  2484.  
  2485.   s = 0;
  2486.  
  2487.   if (pmtl[winner] > 0)
  2488.     for (i = 1; i <= PieceCnt[winner]; i++)
  2489.       s += ScoreKPK (side, winner, loser, king1, king2, PieceList[winner][i]);
  2490.  
  2491.   else if (emtl[winner] == valueB + valueN)
  2492.     s = ScoreKBNK (winner, king1, king2);
  2493.  
  2494.   else if (emtl[winner] > valueB)
  2495.     s = 500 + emtl[winner] - DyingKing[king2] - 2 * distance (king1, king2);
  2496.  
  2497.   if (side == winner)
  2498.     *score = s;
  2499.   else
  2500.     *score = -s;
  2501. }
  2502.  
  2503.  
  2504. static inline void
  2505. BRscan (short int sq, short int *s, short int *mob)
  2506.  
  2507. /*
  2508.   Find Bishop and Rook mobility, XRAY attacks, and pins. Increment the
  2509.   hung[] array if a pin is found.
  2510. */
  2511. {
  2512.   register short u, piece, pin;
  2513.   register unsigned char *ppos, *pdir;
  2514.   short *Kf;
  2515.  
  2516.   Kf = Kfield[c1];
  2517.   *mob = 0;
  2518.   piece = board[sq];
  2519.   ppos = nextpos[piece][sq];
  2520.   pdir = nextdir[piece][sq];
  2521.   u = ppos[sq];
  2522.   pin = -1;            /* start new direction */
  2523.   do
  2524.     {
  2525.       *s += Kf[u];
  2526.       if (color[u] == neutral)
  2527.     {
  2528.       (*mob)++;
  2529.       if (ppos[u] == pdir[u])
  2530.         pin = -1;        /* oops new direction */
  2531.       u = ppos[u];
  2532.     }
  2533.       else if (pin < 0)
  2534.     {
  2535.       if (board[u] == pawn || board[u] == king)
  2536.         u = pdir[u];
  2537.       else
  2538.         {
  2539.           if (ppos[u] != pdir[u])
  2540.         pin = u;    /* not on the edge and on to find a pin */
  2541.           u = ppos[u];
  2542.         }
  2543.     }
  2544.       else
  2545.     {
  2546.       if (color[u] == c2 && (board[u] > piece || atk2[u] == 0))
  2547.         {
  2548.           if (color[pin] == c2)
  2549.         {
  2550.           *s += PINVAL;
  2551.           if (atk2[pin] == 0 ||
  2552.               atk1[pin] > control[board[pin]] + 1)
  2553.             ++hung[c2];
  2554.         }
  2555.           else
  2556.         *s += XRAY;
  2557.         }
  2558.       pin = -1;        /* new direction */
  2559.       u = pdir[u];
  2560.     }
  2561.   } while (u != sq);
  2562. }
  2563.  
  2564.  
  2565. static inline void
  2566. KingScan (short int sq, short int *s)
  2567.  
  2568. /*
  2569.   Assign penalties if king can be threatened by checks, if squares
  2570.   near the king are controlled by the enemy (especially the queen),
  2571.   or if there are no pawns near the king.
  2572.   The following must be true:
  2573.   board[sq] == king
  2574.   c1 == color[sq]
  2575.   c2 == otherside[c1]
  2576. */
  2577.  
  2578. #define ScoreThreat \
  2579. if (color[u] != c2)\
  2580.   if (atk1[u] == 0 || (atk2[u] & 0xFF) > 1) ++cnt;\
  2581.   else *s -= 3
  2582.  
  2583. {
  2584.   register short u;
  2585.   register unsigned char *ppos, *pdir;
  2586.   register short cnt, ok;
  2587.  
  2588.   cnt = 0;
  2589.   if (HasBishop[c2] || HasQueen[c2])
  2590.     {
  2591.       ppos = nextpos[bishop][sq];
  2592.       pdir = nextdir[bishop][sq];
  2593.       u = ppos[sq];
  2594.       do
  2595.     {
  2596.       if (atk2[u] & ctlBQ)
  2597.         ScoreThreat;
  2598.       u = (color[u] == neutral) ? ppos[u] : pdir[u];
  2599.       } while (u != sq);
  2600.     }
  2601.   if (HasRook[c2] || HasQueen[c2])
  2602.     {
  2603.       ppos = nextpos[rook][sq];
  2604.       pdir = nextdir[rook][sq];
  2605.       u = ppos[sq];
  2606.       do
  2607.     {
  2608.       if (atk2[u] & ctlRQ)
  2609.         ScoreThreat;
  2610.       u = (color[u] == neutral) ? ppos[u] : pdir[u];
  2611.       } while (u != sq);
  2612.     }
  2613.   if (HasKnight[c2])
  2614.     {
  2615.       pdir = nextdir[knight][sq];
  2616.       u = pdir[sq];
  2617.       do
  2618.     {
  2619.       if (atk2[u] & ctlNN)
  2620.         ScoreThreat;
  2621.       u = pdir[u];
  2622.       } while (u != sq);
  2623.     }
  2624.   *s += (KSFTY * KTHRT[cnt]) / 16;
  2625.  
  2626.   cnt = 0;
  2627.   ok = false;
  2628.   pdir = nextpos[king][sq];
  2629.   u = pdir[sq];
  2630.   do
  2631.     {
  2632.       if (board[u] == pawn)
  2633.     ok = true;
  2634.       if (atk2[u] > atk1[u])
  2635.     {
  2636.       ++cnt;
  2637.       if (atk2[u] & ctlQ)
  2638.         if (atk2[u] > ctlQ + 1 && atk1[u] < ctlQ)
  2639.           *s -= 4 * KSFTY;
  2640.     }
  2641.       u = pdir[u];
  2642.   } while (u != sq);
  2643.   if (!ok)
  2644.     *s -= KSFTY;
  2645.   if (cnt > 1)
  2646.     *s -= KSFTY;
  2647. }
  2648.  
  2649.  
  2650. static inline int
  2651. trapped (short int sq)
  2652.  
  2653. /*
  2654.   See if the attacked piece has unattacked squares to move to.
  2655.   The following must be true:
  2656.   c1 == color[sq]
  2657.   c2 == otherside[c1]
  2658. */
  2659.  
  2660. {
  2661.   register short u, piece;
  2662.   register unsigned char *ppos, *pdir;
  2663.  
  2664.   piece = board[sq];
  2665.   ppos = nextpos[ptype[c1][piece]][sq];
  2666.   pdir = nextdir[ptype[c1][piece]][sq];
  2667.   if (piece == pawn)
  2668.     {
  2669.       u = ppos[sq];    /* follow no captures thread */
  2670.       if (color[u] == neutral)
  2671.     {
  2672.       if (atk1[u] >= atk2[u])
  2673.         return (false);
  2674.       if (atk2[u] < ctlP)
  2675.         {
  2676.           u = ppos[u];
  2677.           if (color[u] == neutral && atk1[u] >= atk2[u])
  2678.         return (false);
  2679.         }
  2680.     }
  2681.       u = pdir[sq];    /* follow captures thread */
  2682.       if (color[u] == c2)
  2683.     return (false);
  2684.       u = pdir[u];
  2685.       if (color[u] == c2)
  2686.     return (false);
  2687.     }
  2688.   else
  2689.     {
  2690.       u = ppos[sq];
  2691.       do
  2692.     {
  2693.       if (color[u] != c1)
  2694.         if (atk2[u] == 0 || board[u] >= piece)
  2695.           return (false);
  2696.       u = (color[u] == neutral) ? ppos[u] : pdir[u];
  2697.       } while (u != sq);
  2698.     }
  2699.   return (true);
  2700. }
  2701.  
  2702.  
  2703. static inline int
  2704. PawnValue (short int sq, short int side)
  2705.  
  2706. /*
  2707.   Calculate the positional value for a pawn on 'sq'.
  2708. */
  2709.  
  2710. {
  2711.   register short j, fyle, rank;
  2712.   register short s, a1, a2, in_square, r, e;
  2713.  
  2714.   a1 = (atk1[sq] & 0x4FFF);
  2715.   a2 = (atk2[sq] & 0x4FFF);
  2716.   rank = row (sq);
  2717.   fyle = column (sq);
  2718.   s = 0;
  2719.   if (c1 == white)
  2720.     {
  2721.       s = Mwpawn[sq];
  2722.       if ((sq == 11 && color[19] != neutral)
  2723.           || (sq == 12 && color[20] != neutral))
  2724.     s += PEDRNK2B;
  2725.       if ((fyle == 0 || PC1[fyle - 1] == 0)
  2726.       && (fyle == 7 || PC1[fyle + 1] == 0))
  2727.     s += ISOLANI[fyle];
  2728.       else if (PC1[fyle] > 1)
  2729.     s += PDOUBLED;
  2730.       if (a1 < ctlP && atk1[sq + 8] < ctlP)
  2731.     {
  2732.       s += BACKWARD[a2 & 0xFF];
  2733.       if (PC2[fyle] == 0)
  2734.         s += PWEAKH;
  2735.       if (color[sq + 8] != neutral)
  2736.         s += PBLOK;
  2737.     }
  2738.       if (PC2[fyle] == 0)
  2739.     {
  2740.       if (side == black)
  2741.         r = rank - 1;
  2742.       else
  2743.         r = rank;
  2744.       in_square = (row (bking) >= r && distance (sq, bking) < 8 - r);
  2745.       if (a2 == 0 || side == white)
  2746.         e = 0;
  2747.       else
  2748.         e = 1;
  2749.       for (j = sq + 8; j < 64; j += 8)
  2750.         if (atk2[j] >= ctlP)
  2751.           {
  2752.         e = 2;
  2753.         break;
  2754.           }
  2755.         else if (atk2[j] > 0 || color[j] != neutral)
  2756.           e = 1;
  2757.       if (e == 2)
  2758.         s += (stage * PassedPawn3[rank]) / 10;
  2759.       else if (in_square || e == 1)
  2760.         s += (stage * PassedPawn2[rank]) / 10;
  2761.       else if (emtl[black] > 0)
  2762.         s += (stage * PassedPawn1[rank]) / 10;
  2763.       else
  2764.         s += PassedPawn0[rank];
  2765.     }
  2766.     }
  2767.   else if (c1 == black)
  2768.     {
  2769.       s = Mbpawn[sq];
  2770.       if ((sq == 51 && color[43] != neutral)
  2771.       || (sq == 52 && color[44] != neutral))
  2772.     s += PEDRNK2B;
  2773.       if ((fyle == 0 || PC1[fyle - 1] == 0) &&
  2774.       (fyle == 7 || PC1[fyle + 1] == 0))
  2775.     s += ISOLANI[fyle];
  2776.       else if (PC1[fyle] > 1)
  2777.     s += PDOUBLED;
  2778.       if (a1 < ctlP && atk1[sq - 8] < ctlP)
  2779.     {
  2780.       s += BACKWARD[a2 & 0xFF];
  2781.       if (PC2[fyle] == 0)
  2782.         s += PWEAKH;
  2783.       if (color[sq - 8] != neutral)
  2784.         s += PBLOK;
  2785.     }
  2786.       if (PC2[fyle] == 0)
  2787.     {
  2788.       if (side == white)
  2789.         r = rank + 1;
  2790.       else
  2791.         r = rank;
  2792.       in_square = (row (wking) <= r && distance (sq, wking) < r + 1);
  2793.       if (a2 == 0 || side == black)
  2794.         e = 0;
  2795.       else
  2796.         e = 1;
  2797.       for (j = sq - 8; j >= 0; j -= 8)
  2798.         if (atk2[j] >= ctlP)
  2799.           {
  2800.         e = 2;
  2801.         break;
  2802.           }
  2803.         else if (atk2[j] > 0 || color[j] != neutral)
  2804.           e = 1;
  2805.       if (e == 2)
  2806.         s += (stage * PassedPawn3[7 - rank]) / 10;
  2807.       else if (in_square || e == 1)
  2808.         s += (stage * PassedPawn2[7 - rank]) / 10;
  2809.       else if (emtl[white] > 0)
  2810.         s += (stage * PassedPawn1[7 - rank]) / 10;
  2811.       else
  2812.         s += PassedPawn0[7 - rank];
  2813.     }
  2814.     }
  2815.   if (a2 > 0)
  2816.     {
  2817.       if (a1 == 0 || a2 > ctlP + 1)
  2818.     {
  2819.       s += HUNGP;
  2820.       ++hung[c1];
  2821.       if (trapped (sq))
  2822.         ++hung[c1];
  2823.     }
  2824.       else
  2825.     if (a2 > a1)
  2826.       s += ATAKD;
  2827.     }
  2828.   return (s);
  2829. }
  2830.  
  2831.  
  2832. static inline int
  2833. KnightValue (short int sq, short int side)
  2834.  
  2835. /*
  2836.   Calculate the positional value for a knight on 'sq'.
  2837. */
  2838.  
  2839. {
  2840.   register short s, a2, a1;
  2841.  
  2842.   s = Mknight[c1][sq];
  2843.   a2 = (atk2[sq] & 0x4FFF);
  2844.   if (a2 > 0)
  2845.     {
  2846.       a1 = (atk1[sq] & 0x4FFF);
  2847.       if (a1 == 0 || a2 > ctlBN + 1)
  2848.     {
  2849.       s += HUNGP;
  2850.       ++hung[c1];
  2851.       if (trapped (sq))
  2852.         ++hung[c1];
  2853.     }
  2854.       else
  2855.     if (a2 >= ctlBN || a1 < ctlP)
  2856.       s += ATAKD;
  2857.     }
  2858.   return (s);
  2859. }
  2860.  
  2861.  
  2862. static inline int
  2863. BishopValue (short int sq, short int side)
  2864.  
  2865. /*
  2866.   Calculate the positional value for a bishop on 'sq'.
  2867. */
  2868.  
  2869. {
  2870.   register short a2, a1;
  2871.   short s, mob;
  2872.  
  2873.   s = Mbishop[c1][sq];
  2874.   BRscan (sq, &s, &mob);
  2875.   s += BMBLTY[mob];
  2876.   a2 = (atk2[sq] & 0x4FFF);
  2877.   if (a2 > 0)
  2878.     {
  2879.       a1 = (atk1[sq] & 0x4FFF);
  2880.       if (a1 == 0 || a2 > ctlBN + 1)
  2881.     {
  2882.       s += HUNGP;
  2883.       ++hung[c1];
  2884.       if (trapped (sq))
  2885.         ++hung[c1];
  2886.     }
  2887.       else
  2888.     if (a2 >= ctlBN || a1 < ctlP)
  2889.       s += ATAKD;
  2890.     }
  2891.   return (s);
  2892. }
  2893.  
  2894.  
  2895. static inline int
  2896. RookValue (short int sq, short int side)
  2897.  
  2898. /*
  2899.   Calculate the positional value for a rook on 'sq'.
  2900. */
  2901.  
  2902. {
  2903.   register short fyle, a2, a1;
  2904.   short s, mob;
  2905.  
  2906.   s = RookBonus;
  2907.   BRscan (sq, &s, &mob);
  2908.   s += RMBLTY[mob];
  2909.   fyle = column (sq);
  2910.   if (PC1[fyle] == 0)
  2911.     s += RHOPN;
  2912.   if (PC2[fyle] == 0)
  2913.     s += RHOPNX;
  2914.   if (pmtl[c2] > 100 && row (sq) == rank7[c1])
  2915.     s += 10;
  2916.   if (stage > 2)
  2917.     s += 14 - taxicab (sq, EnemyKing);
  2918.   a2 = (atk2[sq] & 0x4FFF);
  2919.   if (a2 > 0)
  2920.     {
  2921.       a1 = (atk1[sq] & 0x4FFF);
  2922.       if (a1 == 0 || a2 > ctlR + 1)
  2923.     {
  2924.       s += HUNGP;
  2925.       ++hung[c1];
  2926.  
  2927.       if (trapped (sq))
  2928.         ++hung[c1];
  2929.     }
  2930.       else
  2931.     if (a2 >= ctlR || a1 < ctlP)
  2932.       s += ATAKD;
  2933.     }
  2934.   return (s);
  2935. }
  2936.  
  2937.  
  2938. static inline int
  2939. QueenValue (short int sq, short int side)
  2940.  
  2941. /*
  2942.   Calculate the positional value for a queen on 'sq'.
  2943. */
  2944.  
  2945. {
  2946.   register short s, a2, a1;
  2947.  
  2948.   s = (distance (sq, EnemyKing) < 3) ? 12 : 0;
  2949.   if (stage > 2)
  2950.     s += 14 - taxicab (sq, EnemyKing);
  2951.   a2 = (atk2[sq] & 0x4FFF);
  2952.   if (a2 > 0)
  2953.     {
  2954.       a1 = (atk1[sq] & 0x4FFF);
  2955.       if (a1 == 0 || a2 > ctlQ + 1)
  2956.     {
  2957.       s += HUNGP;
  2958.       ++hung[c1];
  2959.       if (trapped (sq))
  2960.         ++hung[c1];
  2961.     }
  2962.       else
  2963.     if (a2 >= ctlQ || a1 < ctlP)
  2964.       s += ATAKD;
  2965.     }
  2966.   return (s);
  2967. }
  2968.  
  2969.  
  2970. static inline int
  2971. KingValue (short int sq, short int side)
  2972.  
  2973. /*
  2974.   Calculate the positional value for a king on 'sq'.
  2975. */
  2976.  
  2977. {
  2978.   register short fyle, a2, a1;
  2979.   short s;
  2980.  
  2981.   s = Mking[c1][sq];
  2982.   if (KSFTY > 0)
  2983.     if (Developed[c2] || stage > 0)
  2984.       KingScan (sq, &s);
  2985.   if (castld[c1])
  2986.     s += KCASTLD;
  2987.   else if (Mvboard[kingP[c1]])
  2988.     s += KMOVD;
  2989.  
  2990.   fyle = column (sq);
  2991.   if (PC1[fyle] == 0)
  2992.     s += KHOPN;
  2993.   if (PC2[fyle] == 0)
  2994.     s += KHOPNX;
  2995.   switch (fyle)
  2996.     {
  2997.     case 5:
  2998.       if (PC1[7] == 0)
  2999.         s += KHOPN;
  3000.       if (PC2[7] == 0)
  3001.         s += KHOPNX;
  3002.       /* Fall through */
  3003.     case 4:
  3004.     case 6:
  3005.     case 0:
  3006.       if (PC1[fyle + 1] == 0)
  3007.         s += KHOPN;
  3008.       if (PC2[fyle + 1] == 0)
  3009.         s += KHOPNX;
  3010.       break;
  3011.     case 2:    
  3012.       if (PC1[0] == 0)
  3013.         s += KHOPN;
  3014.       if (PC2[0] == 0)
  3015.         s += KHOPNX;
  3016.       /* Fall through */
  3017.     case 3:
  3018.     case 1:
  3019.     case 7:  
  3020.       if (PC1[fyle - 1] == 0)
  3021.         s += KHOPN;
  3022.       if (PC2[fyle - 1] == 0)
  3023.         s += KHOPNX;
  3024.       break;
  3025.     default:
  3026.       /* Impossible! */
  3027.       break;
  3028.     }
  3029.  
  3030.   a2 = (atk2[sq] & 0x4FFF);
  3031.   if (a2 > 0)
  3032.     {
  3033.       a1 = (atk1[sq] & 0x4FFF);
  3034.       if (a1 == 0 || a2 > ctlK + 1)
  3035.     {
  3036.       s += HUNGP;
  3037.       ++hung[c1];
  3038.     }
  3039.       else
  3040.     s += ATAKD;
  3041.     }
  3042.   return (s);
  3043. }
  3044.  
  3045.  
  3046. void
  3047. ScorePosition (short int side, short int *score)
  3048.  
  3049. /*
  3050.   Perform normal static evaluation of board position. A score is generated
  3051.   for each piece and these are summed to get a score for each side.
  3052. */
  3053.  
  3054. {
  3055.   register short sq, s, i, xside;
  3056.   short pscore[2];
  3057.  
  3058.   UpdateWeights ();
  3059.   xside = otherside[side];
  3060.   pscore[white] = pscore[black] = 0;
  3061.  
  3062.   for (c1 = white; c1 <= black; c1++)
  3063.     {
  3064.       c2 = otherside[c1];
  3065.       atk1 = atak[c1];
  3066.       atk2 = atak[c2];
  3067.       PC1 = PawnCnt[c1];
  3068.       PC2 = PawnCnt[c2];
  3069.       for (i = PieceCnt[c1]; i >= 0; i--)
  3070.     {
  3071.       sq = PieceList[c1][i];
  3072.       switch (board[sq])
  3073.         {
  3074.         case pawn:
  3075.           s = PawnValue(sq, side);
  3076.           break;
  3077.         case knight:
  3078.           s = KnightValue(sq, side);
  3079.           break;
  3080.         case bishop:
  3081.           s = BishopValue(sq, side);
  3082.           break;
  3083.         case rook:
  3084.           s = RookValue(sq, side);
  3085.           break;
  3086.         case queen:
  3087.           s = QueenValue(sq, side);
  3088.           break;
  3089.         case king:
  3090.           s = KingValue(sq, side);
  3091.           break;
  3092.         default:
  3093.           s = 0;
  3094.           break;
  3095.         }
  3096.       pscore[c1] += s;
  3097.       svalue[sq] = s;
  3098.     }
  3099.     }
  3100.   if (hung[side] > 1)
  3101.     pscore[side] += HUNGX;
  3102.   if (hung[xside] > 1)
  3103.     pscore[xside] += HUNGX;
  3104.  
  3105.   *score = mtl[side] - mtl[xside] + pscore[side] - pscore[xside] + 10;
  3106.   if (dither)
  3107.     *score += urand () % dither;
  3108.  
  3109.   if (*score > 0 && pmtl[side] == 0)
  3110.     if (emtl[side] < valueR)
  3111.       *score = 0;
  3112.     else if (*score < valueR)
  3113.       *score /= 2;
  3114.   if (*score < 0 && pmtl[xside] == 0)
  3115.     if (emtl[xside] < valueR)
  3116.       *score = 0;
  3117.     else if (-*score < valueR)
  3118.       *score /= 2;
  3119.  
  3120.   if (mtl[xside] == valueK && emtl[side] > valueB)
  3121.     *score += 200;
  3122.   if (mtl[side] == valueK && emtl[xside] > valueB)
  3123.     *score -= 200;
  3124. }
  3125.  
  3126.  
  3127. static inline void
  3128. BlendBoard (const short int a[64], const short int b[64], short int c[64])
  3129. {
  3130.   register int sq;
  3131.  
  3132.   for (sq = 0; sq < 64; sq++)
  3133.     c[sq] = (a[sq] * (10 - stage) + b[sq] * stage) / 10;
  3134. }
  3135.  
  3136.  
  3137. static inline void
  3138. CopyBoard (const short int a[64], short int b[64])
  3139. {
  3140.   register int sq;
  3141.  
  3142.   for (sq = 0; sq < 64; sq++)
  3143.     b[sq] = a[sq];
  3144. }
  3145.  
  3146. void
  3147. ExaminePosition (void)
  3148.  
  3149. /*
  3150.   This is done one time before the search is started. Set up arrays
  3151.   Mwpawn, Mbpawn, Mknight, Mbishop, Mking which are used in the
  3152.   SqValue() function to determine the positional value of each piece.
  3153. */
  3154.  
  3155. {
  3156.   register short i, sq;
  3157.   short wpadv, bpadv, wstrong, bstrong, z, side, pp, j, k, val, Pd, fyle, rank;
  3158.   static short PawnStorm = false;
  3159.  
  3160.   ataks (white, atak[white]);
  3161.   ataks (black, atak[black]);
  3162.   UpdateWeights ();
  3163.   HasKnight[white] = HasKnight[black] = 0;
  3164.   HasBishop[white] = HasBishop[black] = 0;
  3165.   HasRook[white] = HasRook[black] = 0;
  3166.   HasQueen[white] = HasQueen[black] = 0;
  3167.   for (side = white; side <= black; side++)
  3168.     for (i = PieceCnt[side]; i >= 0; i--)
  3169.       switch (board[PieceList[side][i]])
  3170.     {
  3171.     case knight:
  3172.       ++HasKnight[side];
  3173.       break;
  3174.     case bishop:
  3175.       ++HasBishop[side];
  3176.       break;
  3177.     case rook:
  3178.       ++HasRook[side];
  3179.       break;
  3180.     case queen:
  3181.       ++HasQueen[side];
  3182.       break;
  3183.     }
  3184.   if (!Developed[white])
  3185.     Developed[white] = (board[1] != knight && board[2] != bishop &&
  3186.             board[5] != bishop && board[6] != knight);
  3187.   if (!Developed[black])
  3188.     Developed[black] = (board[57] != knight && board[58] != bishop &&
  3189.             board[61] != bishop && board[62] != knight);
  3190.   if (!PawnStorm && stage < 5)
  3191.     PawnStorm = ((column (wking) < 3 && column (bking) > 4) ||
  3192.          (column (wking) > 4 && column (bking) < 3));
  3193.  
  3194.   CopyBoard (pknight, Mknight[white]);
  3195.   CopyBoard (pknight, Mknight[black]);
  3196.   CopyBoard (pbishop, Mbishop[white]);
  3197.   CopyBoard (pbishop, Mbishop[black]);
  3198.   BlendBoard (KingOpening, KingEnding, Mking[white]);
  3199.   BlendBoard (KingOpening, KingEnding, Mking[black]);
  3200.  
  3201.   for (sq = 0; sq < 64; sq++)
  3202.     {
  3203.       fyle = column (sq);
  3204.       rank = row (sq);
  3205.       wstrong = bstrong = true;
  3206.       for (i = sq; i < 64; i += 8)
  3207.     if (Patak (black, i))
  3208.       {
  3209.         wstrong = false;
  3210.         break;
  3211.       }
  3212.       for (i = sq; i >= 0; i -= 8)
  3213.     if (Patak (white, i))
  3214.       {
  3215.         bstrong = false;
  3216.         break;
  3217.       }
  3218.       wpadv = bpadv = PADVNCM;
  3219.       if ((fyle == 0 || PawnCnt[white][fyle - 1] == 0) &&
  3220.       (fyle == 7 || PawnCnt[white][fyle + 1] == 0))
  3221.     wpadv = PADVNCI;
  3222.       if ((fyle == 0 || PawnCnt[black][fyle - 1] == 0) &&
  3223.       (fyle == 7 || PawnCnt[black][fyle + 1] == 0))
  3224.     bpadv = PADVNCI;
  3225.       Mwpawn[sq] = (wpadv * PawnAdvance[sq]) / 10;
  3226.       Mbpawn[sq] = (bpadv * PawnAdvance[63 - sq]) / 10;
  3227.       Mwpawn[sq] += PawnBonus;
  3228.       Mbpawn[sq] += PawnBonus;
  3229.       if (Mvboard[kingP[white]])
  3230.     {
  3231.       if ((fyle < 3 || fyle > 4) && distance (sq, wking) < 3)
  3232.         Mwpawn[sq] += PAWNSHIELD;
  3233.     }
  3234.       else if (rank < 3 && (fyle < 2 || fyle > 5))
  3235.     Mwpawn[sq] += PAWNSHIELD / 2;
  3236.       if (Mvboard[kingP[black]])
  3237.     {
  3238.       if ((fyle < 3 || fyle > 4) && distance (sq, bking) < 3)
  3239.         Mbpawn[sq] += PAWNSHIELD;
  3240.     }
  3241.       else if (rank > 4 && (fyle < 2 || fyle > 5))
  3242.     Mbpawn[sq] += PAWNSHIELD / 2;
  3243.       if (PawnStorm)
  3244.     {
  3245.       if ((column (wking) < 4 && fyle > 4) ||
  3246.           (column (wking) > 3 && fyle < 3))
  3247.         Mwpawn[sq] += 3 * rank - 21;
  3248.       if ((column (bking) < 4 && fyle > 4) ||
  3249.           (column (bking) > 3 && fyle < 3))
  3250.         Mbpawn[sq] -= 3 * rank;
  3251.     }
  3252.       Mknight[white][sq] += 5 - distance (sq, bking);
  3253.       Mknight[white][sq] += 5 - distance (sq, wking);
  3254.       Mknight[black][sq] += 5 - distance (sq, wking);
  3255.       Mknight[black][sq] += 5 - distance (sq, bking);
  3256.       Mbishop[white][sq] += BishopBonus;
  3257.       Mbishop[black][sq] += BishopBonus;
  3258.       for (i = PieceCnt[black]; i >= 0; i--)
  3259.     if (distance (sq, PieceList[black][i]) < 3)
  3260.       Mknight[white][sq] += KNIGHTPOST;
  3261.       for (i = PieceCnt[white]; i >= 0; i--)
  3262.     if (distance (sq, PieceList[white][i]) < 3)
  3263.       Mknight[black][sq] += KNIGHTPOST;
  3264.       if (wstrong)
  3265.     Mknight[white][sq] += KNIGHTSTRONG;
  3266.       if (bstrong)
  3267.     Mknight[black][sq] += KNIGHTSTRONG;
  3268.       if (wstrong)
  3269.     Mbishop[white][sq] += BISHOPSTRONG;
  3270.       if (bstrong)
  3271.     Mbishop[black][sq] += BISHOPSTRONG;
  3272.  
  3273.       if (HasBishop[white] == 2)
  3274.     Mbishop[white][sq] += 8;
  3275.       if (HasBishop[black] == 2)
  3276.     Mbishop[black][sq] += 8;
  3277.       if (HasKnight[white] == 2)
  3278.     Mknight[white][sq] += 5;
  3279.       if (HasKnight[black] == 2)
  3280.     Mknight[black][sq] += 5;
  3281.  
  3282.       Kfield[white][sq] = Kfield[black][sq] = 0;
  3283.       if (distance (sq, wking) == 1)
  3284.     Kfield[black][sq] = KATAK;
  3285.       if (distance (sq, bking) == 1)
  3286.     Kfield[white][sq] = KATAK;
  3287.  
  3288.       Pd = 0;
  3289.       for (k = 0; k <= PieceCnt[white]; k++)
  3290.     {
  3291.       i = PieceList[white][k];
  3292.       if (board[i] == pawn)
  3293.         {
  3294.           pp = true;
  3295.           if (row (i) == 6)
  3296.         z = i + 8;
  3297.           else
  3298.         z = i + 16;
  3299.           for (j = i + 8; j < 64; j += 8)
  3300.         if (Patak (black, j) || board[j] == pawn)
  3301.           {
  3302.             pp = false;
  3303.             break;
  3304.           }
  3305.           if (pp)
  3306.         Pd += 5 * taxicab (sq, z);
  3307.           else
  3308.         Pd += taxicab (sq, z);
  3309.         }
  3310.     }
  3311.       for (k = 0; k <= PieceCnt[black]; k++)
  3312.     {
  3313.       i = PieceList[black][k];
  3314.       if (board[i] == pawn)
  3315.         {
  3316.           pp = true;
  3317.           if (row (i) == 1)
  3318.         z = i - 8;
  3319.           else
  3320.         z = i - 16;
  3321.           for (j = i - 8; j >= 0; j -= 8)
  3322.         if (Patak (white, j) || board[j] == pawn)
  3323.           {
  3324.             pp = false;
  3325.             break;
  3326.           }
  3327.           if (pp)
  3328.         Pd += 5 * taxicab (sq, z);
  3329.           else
  3330.         Pd += taxicab (sq, z);
  3331.         }
  3332.     }
  3333.       if (Pd != 0)
  3334.     {
  3335.       val = (Pd * stage2) / 10;
  3336.       Mking[white][sq] -= val;
  3337.       Mking[black][sq] -= val;
  3338.     }
  3339.     }
  3340. }
  3341.  
  3342. void
  3343. UpdateWeights (void)
  3344.  
  3345. /*
  3346.   If material balance has changed, determine the values for the positional
  3347.   evaluation terms.
  3348. */
  3349.  
  3350. {
  3351.   register short tmtl, s1;
  3352.  
  3353.   emtl[white] = mtl[white] - pmtl[white] - valueK;
  3354.   emtl[black] = mtl[black] - pmtl[black] - valueK;
  3355.   tmtl = emtl[white] + emtl[black];
  3356.   s1 = (tmtl > 6600) ? 0 : ((tmtl < 1400) ? 10 : (6600 - tmtl) / 520);
  3357.   if (s1 != stage)
  3358.     {
  3359.       stage = s1;
  3360.       stage2 = (tmtl > 3600) ? 0 : ((tmtl < 1400) ? 10 : (3600 - tmtl) / 220);
  3361.       PEDRNK2B = -15;    /* centre pawn on 2nd rank & blocked */
  3362.       PBLOK = -4;        /* blocked backward pawn */
  3363.       PDOUBLED = -14;    /* doubled pawn */
  3364.       PWEAKH = -4;        /* weak pawn on half open file */
  3365.       PAWNSHIELD = 10 - stage;    /* pawn near friendly king */
  3366.       PADVNCM = 10;        /* advanced pawn multiplier */
  3367.       PADVNCI = 7;        /* muliplier for isolated pawn */
  3368.       PawnBonus = stage;
  3369.       
  3370.       KNIGHTPOST = (stage + 2) / 3;    /* knight near enemy pieces */
  3371.       KNIGHTSTRONG = (stage + 6) / 2;    /* occupies pawn hole */
  3372.       
  3373.       BISHOPSTRONG = (stage + 6) / 2;    /* occupies pawn hole */
  3374.       BishopBonus = 2 * stage;
  3375.       
  3376.       RHOPN = 10;        /* rook on half open file */
  3377.       RHOPNX = 4;
  3378.       RookBonus = 6 * stage;
  3379.       
  3380.       XRAY = 8;        /* Xray attack on piece */
  3381.       PINVAL = 10;        /* Pin */
  3382.       
  3383.       KHOPN = (3 * stage - 30) / 2;    /* king on half open file */
  3384.       KHOPNX = KHOPN / 2;
  3385.       KCASTLD = 10 - stage;
  3386.       KMOVD = -40 / (stage + 1);    /* king moved before castling */
  3387.       KATAK = (10 - stage) / 2;    /* B,R attacks near enemy king */
  3388.       if (stage < 8)
  3389.     KSFTY = 16 - 2 * stage;
  3390.       else
  3391.     KSFTY = 0;
  3392.       
  3393.       ATAKD = -6;        /* defender > attacker */
  3394.       HUNGP = -8;        /* each hung piece */
  3395.       HUNGX = -12;        /* extra for >1 hung piece */
  3396.     }
  3397. }
  3398.